Mastering Power Bi 2nd Edition Chandraish Sinha
Mastering Power Bi 2nd Edition Chandraish Sinha
Mastering
Power BI
Build business intelligence applications powered
with DAX calculations, insightful
visualizations, advanced BI
techniques, and loads of data sources
2nd Edition
Chandraish Sinha
www.bpbonline.com
ii
Second Revised and Updated Edition 2024
ISBN: 978-93-55517-166
All Rights Reserved. No part of this publication may be reproduced, distributed or transmitted in
any form or by any means or stored in a database or retrieval system, without the prior written
permission of the publisher with the exception to the program listings which may be entered,
stored and executed in a computer system, but they can not be reproduced by the means of
publication, photocopy, recording, or by any electronic and mechanical means.
All trademarks referred to in the book are acknowledged as properties of their respective
owners but BPB Publications cannot guarantee the accuracy of this information.
www.bpbonline.com
iii
Dedicated to
Ishie
My daughter, who taught me that patience is a virtue
iv
Chandraish Sinha is the Founder and President of Ohio Computer Academy, a company
dedicated to providing IT education.
v Preeti Sahu is an expert in Power BI and has an extensive background working with
SharePoint and the Power Platform. She is currently employed by TSinfo Technologies
as a Power Platform specialist for Power BI, Power Apps, Power Automate, and Power
Virtual Agents. She is the author of the Microsoft Power Platform A Deep Dive book
and the co-author of the SharePoint Online Modern Experience Practical Guide book
(BPB Publications). Also, she has contributed a significant amount of time on technical
articles on the Power Platform to SPGuides.com and EnjoySharePoint.com. In addition
to traveling, she enjoys spending time with her family during her free time.
v Vivek Menon M is an AI and Data Science professional with six years of experience
in the industry. In his independent career, he has worked in advanced areas of NLP,
Computer Vision, Generative AI, Data Analytics, Python, Machine Learning, etc., to
gain comprehensive knowledge and skills in the field. As an eloquent speaker, Vivek
frequently presents seminars, and workshops to share his insights and experience in
the field, thereby adding value to the broader AI community. He enjoys the natural
vi
ability to break down even the most complex topics to make them understandable
and accommodative among listeners, strengths that make him a compelling and
authoritative speaker. Beyond a successful job, Vivek is a reader, a characteristic that
allows him to learn a wide range of subjects, from computational theories to classic
books. His diverse learning style allows him to mold an integrative approach to AI
implementations that foster success. He is also involved in mentoring activities for
young data scientists, reflecting that he is committed to educating and mentoring
young professionals to mold their future as suitable data science professionals.
vii
Acknowledgement
I extend my sincere gratitude to all my readers who supported the first edition of Mastering
Power BI. Your feedback and responses guided me in crafting the second edition of this
book.
I am deeply appreciative of all my readers who have supported my various books and
motivated me to continue writing. I carefully consider each review and feedback provided,
and your encouragement fuels my passion for sharing knowledge.
A special thank you to Preeti, Vivek, and Sriram, the technical reviewers of this book, for
meticulously reviewing the entire manuscript and offering invaluable suggestions.
Lastly, I express my gratitude to BPB Publications for granting me this opportunity. Writing
a book involves the collaboration of many individuals, and I am thankful to everyone
involved for their support and assistance throughout this endeavor.
viii
Preface
Welcome to the second edition of Mastering Power BI. Since the release of the first edition,
the world of data analytics has continued to evolve at a rapid pace. Power BI desktop
has introduced new tools, techniques, and features, reshaping the way we analyze and
visualize data. In this dynamic landscape, staying ahead requires not just keeping up with
the latest advancements, but also mastering the core principles that underpin them.
This book provides a comprehensive guide in unleashing the full potential of Power BI.
Whether you are a seasoned data professional or just beginning your journey into the
world of business intelligence, this book is designed to equip you with the knowledge and
skills needed to succeed.
In this second edition, we have updated and expanded upon the content of the first edition
to ensure it remains relevant and valuable in today's data-driven world. It incorporates
new features and functionalities of Power BI, explores advanced data transformation
techniques, covers Data Analysis Expressions (DAX), introduces the much-needed
Artificial Intelligence (AI) concepts, and offers practical step-by-step exercises to illustrate
all the concepts.
Throughout the book, readers will find step-by-step instructions and hands-on exercises
designed to reinforce learning and empower them to tackle real-world challenges with
confidence. Whether you are building robust data models, creating visually stunning
reports and dashboards, or uncovering hidden data insights using AI, Mastering Power BI
will be your trusted companion on your journey to becoming a Power BI expert.
The primary challenge I faced in writing this book was the changes introduced by Power
BI. A few times, the look-and-feel of the application has changed. I have tried my best to
keep up with the changes but in case you find some discrepancies, remember the changes
are only in the interface not in the concepts.
With this book, you will gain the knowledge and skills to become a proficient Power BI
developer.
I invite you to explore the possibilities, and embark on a transformative journey with
Power BI. Let us unlock the full potential of your data together. I hope you will find this
book informative and helpful.
ix
Chapter 1: Understanding the Basics – This chapter covers the basics of Business
Intelligence and explains all the important terms and definitions. It also explains the
different components of Power BI and what they do. It will assist you in installing the
Power BI desktop and provide an overview of the data tables used in the book.
Chapter 2: Connect and Shape – This chapter explains how Power BI connects to data
from disparate sources, such as database tables, XLS files, relational database and many
more. It also introduces you to Query Editor and describes how to use it to shape your
data.
Chapter 4: Optimize Your Data Model – This chapter deals with creating and optimizing
a data model in Power BI. It covers relationships and how to create them in Power BI. It
also explains how to create joins by using merge and append functionalities.
Chapter 5: Data Analysis Expressions – This chapter explains DAX, which is essential for
any Power BI implementation. The chapter covers the concepts of calculated columns and
measures. Additionally, it explains all the main functions and shows how to implement
them.
Chapter 7: Drill Through and Drill Down Reports – This chapter covers how to get more
insights into the data by using the Drill through and drill down reports in Power BI. It also
elucidates how to create custom data hierarchies.
Chapter 9: Power BI Service – This chapter describes the components of Power BI Service.
You will deploy the application created in the Power BI desktop to Power BI Service. This
chapter also teaches how to connect to data and create visualizations in Power BI Service.
Chapter 10: Securing Your Application – We have introduced the concept of Row-level
security (RLS). It is essential to secure the data to prevent unauthorized access and users
see only the data they are authorized to view.
x
https://rebrand.ly/9h0nxf0
The code bundle for the book is also hosted on GitHub at
https://github.com/bpbpublications/Mastering-Power-BI-2nd-Edition.
In case there’s an update to the code, it will be updated on the existing GitHub repository.
We have code bundles from our rich catalogue of books and videos available at
https://github.com/bpbpublications. Check them out!
Errata
We take immense pride in our work at BPB Publications and follow best practices to
ensure the accuracy of our content to provide with an indulging reading experience to our
subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve
upon human errors, if any, that may have occurred during the publishing processes
involved. To let us maintain the quality and help us reach out to any readers who might be
having difficulties due to any unforeseen errors, please write to us at :
errata@bpbonline.com
Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’
Family.
Did you know that BPB offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.bpbonline.
com and as a print book customer, you are entitled to a discount on the eBook copy.
Get in touch with us at :
business@bpbonline.com for more details.
Piracy
If you come across any illegal copies of our works in any form on the internet,
we would be grateful if you would provide us with the location address or
website name. Please contact us at business@bpbonline.com with a link to
the material.
Reviews
Please leave a review. Once you have read and used this book, why not leave
a review on the site that you purchased it from? Potential readers can then see
and use your unbiased opinion to make purchase decisions. We at BPB can
understand what you think about our products, and our authors can see your
feedback on their book. Thank you!
Table of Contents
Conclusion...................................................................................................................... 102
Questions........................................................................................................................ 103
Answers.......................................................................................................................... 103
ISERROR.................................................................................................................. 146
Exercise 9.................................................................................................................. 146
USERNAME............................................................................................................ 146
Exercise 10................................................................................................................ 146
LOOKUPVALUE...................................................................................................... 147
LOOKUPVALUE: SalesPerson_Location and Sales Person.................................... 147
Hiding the redundant tables..................................................................................... 148
Logical functions........................................................................................................... 150
IF............................................................................................................................... 150
Exercise 11................................................................................................................. 150
And............................................................................................................................ 151
Exercise 12................................................................................................................ 151
Switch........................................................................................................................ 152
Exercise 13................................................................................................................ 152
Filter functions............................................................................................................... 153
Filter.......................................................................................................................... 153
Calculate.................................................................................................................... 153
Exercise 14................................................................................................................ 154
CalculateTable........................................................................................................... 154
Exercise 15................................................................................................................ 155
Optional example...................................................................................................... 155
ALL............................................................................................................................ 156
Exercise 16................................................................................................................ 156
Related....................................................................................................................... 157
Exercise 17................................................................................................................ 157
RelatedTable............................................................................................................... 159
Values........................................................................................................................ 159
Exercise 18................................................................................................................ 159
Date and time functions............................................................................................... 160
Calendar.................................................................................................................... 160
Exercise 19................................................................................................................ 160
DateDiff..................................................................................................................... 161
Exercise 20................................................................................................................ 161
Variables in DAX expressions...................................................................................... 162
VAR statement........................................................................................................... 162
xviii
Exercise 21................................................................................................................ 163
Conclusion...................................................................................................................... 164
Questions........................................................................................................................ 164
Answers.......................................................................................................................... 164
Index...............................................................................................................................253-258
xxii
Understanding the Basics 1
Chapter 1
Understanding the
Basics
Introduction
Welcome to the foundational chapter of Mastering Power BI — a comprehensive guide
designed to empower you with the skills and insights needed to harness the full potential
of Power BI. We will build a solid understanding of the basics, laying the groundwork for
your mastery of this powerful business intelligence tool.
In this chapter, we will learn about the basics of Power BI. Power BI is growing in popularity
due to the functionality it provides to the business users. The chapter will cover the basics of
Power BI as a Business Intelligence application. It will start with the Business Intelligence
(BI) fundamentals and explain the terms and technologies in the BI paradigm. You will
learn about Star Schema, Snowflake schema, and gain more understanding on Dimensions
and Facts the building blocks of any BI application.
We will learn about Power BI and understand how it works. This chapter is important as
it will lay the foundation of all the subsequent chapters in this book.
Structure
In this chapter, we will discuss the following topics:
• Understanding business intelligence
• Concepts of the star and snowflake schema
2 Mastering Power BI
Objectives
Understanding the concepts of business intelligence is a key to success in Power BI.
After completing this chapter, you will be able to explain BI and its terminology, like the
star schema, and the snowflake schema. You will also be able to differentiate between
the dimension and fact tables, which is the key to designing powerful data models and
visualizations. You will also learn what is Power BI and how it works.
Advantages of BI system
An enterprise can drive huge benefits by implementing a BI System, which is as follows:
• Data management: BI system facilitates the collection of data from diverse sources.
This data is stored in an enterprise-wide data warehouse or a data mart. Since the
data is centrally stored, it helps in producing a single version of the truth.
Understanding the Basics 3
Components of BI
Before plunging into a BI application like Power BI, it is important to learn about some of
the BI components.
Data sets
The core use of a BI application is to enhance the understanding of data. Data can come
from disparate sources. It can be sourced from legacy systems, relational database, cloud,
or from various file-based applications, such as Excel, CSV, or flat files. The data can be
unstructured, such as emails or webpages.
Data warehouse
A data warehouse is a process of managing large amounts of data in an organization. It is
designed to assist in the BI tasks, especially in analytics. Given a large amount of historical
data, a data warehouse enables faster data query and analysis. A typical data warehouse
contains historical data, which is derived from a variety of sources, such as operational or
transactional databases.
A data warehouse works as a central repository of the aggregated data from multiple
sources and provides an organization with a single version of truth. Since it contains
historical records, it empowers the data scientists and data analysts in improved decision
making and predictive analysis.
4 Mastering Power BI
Data mart
Data mart is similar to the data warehouse but contains only the specific business data
within an organization. A data warehouse is a central repository of an enterprise-wide
data, while a data mart contains the subset of data pertaining to a specific business or user
function. Data warehouse and the data marts are used for reporting and analysis. A data
mart can be sourced from a data warehouse.
A typical data warehousing environment is shown in the following figure:
The preceding Figure 1.1 shows how the data warehouse and the data marts are created,
and are explained in words as follows:
• An ETL process is the run process to extract the data from the various operational
or transactional databases or tables.
• This data is stored in the staging tables.
Understanding the Basics 5
• A different ETL process is created to extract and transform this data, which is
loaded in the data warehouse database.
• From the data warehouse, separate business or user specific data marts are created.
A data warehouse is typically created for the reporting and analytical needs of the
organization. It helps in the data analyses by reducing the number of tables and joining
the ones that are atypical of an operational database.
Data model
A data model displays how the different data entities are related in a data warehouse
environment. It presents a pictorial format, showing different tables and the relationships
between them. In BI, a data model represents the organization's data and should be
designed for faster data access. It should contain all the data categories, hierarchies, and
filters. The most popular data model used in a data warehouse is the dimensional model,
which is also called the star schema. The two types of tables in a star schema are the
dimension table and the fact table. Let us have a look at them briefly:
• Dimensions and facts: Dimension and facts tables are the main ingredients of
any BI implementation. These tables are used to form the star or the snowflake
schemas, which are designed as part of building a data warehouse or a data mart.
• Dimension table: The dimension tables contain the descriptive or qualitative
attributes of the data. For example, the customer dimension may contain
information about the customer, such as the name, address, contact number, and
so on. The dimension fields usually contain the characters or the textual type of
data. The dimension tables are constructed from the operational or transactional
relational database. The dimension tables contain the primary key with the
respective foreign key in the fact table. A dimension table provides context to a
fact table.
There are different types of dimension tables. Some of the commonly used ones are as
follows:
• Slowly Changing dimensions (SCD): It is a dimension table where the row of the
data in the table varies with time. It is used to track the current and historical data.
SCD is implemented in the following three ways:
• Type1: In type1 SCD, the existing row of data is simply overwritten. No history
is maintained, and the existing data is lost. For example, consider the following
employee record:
o Type2: The type2 SCD keeps the complete history of the data by creating a new
record with the Start date and the End date. Only one record will be active at a
time. This is the most popular way of storing the historical data. For example,
consider the following employee record:
If employee John moves to a new department, HR, a new record will be added to
keep the history:
o Type3: In type3 SCD, the history of the data is maintained by using the Current_
Value and the New_Value columns. It is cumbersome to maintain the history, as
it is limited by the number of columns needed to store the historical data. This
technique is not frequently used. For example, consider the following employee
record: