Categories
Microsoft Power BI

Power BI – What is DAX?

Welcome to the world of data visualisation and analysis with Power BI! In this blog, we’ll dive into the heart of Power BI’s analytical capabilities: DAX measures.

DAX, or Data Analysis eXpressions, is a powerful formula language that enables users to define custom calculations in Power BI reports. Whether you’re a seasoned data analyst or just starting out, understanding DAX measures is key to unlocking insightful business intelligence. Power BI – What is DAX?

So, let’s explore some of the most popular measures, array functions, and time-intelligence functions, and see how they can transform raw data into meaningful insights.

Power BI – What is DAX?
Calculate Measure

Some often-used Power BI DAX measures include:

  1. SUM: calculates the sum of a selected column of data
  2. AVERAGE: calculates the average of a selected column of data
  3. COUNT: counts the number of rows in a selected column of data
  4. MIN/MAX: calculates the minimum or maximum value in a selected column of data
  5. DISTINCTCOUNT: counts the number of unique values in a selected column of data
  6. CALCULATE: modifies the context within which a DAX formula is evaluated
  7. FILTER: filters a table based on specific criteria
  8. RANKX: ranks values in a selected column of data
  9. YEAR/QUARTER/MONTH: extracts various time periods from a date column in a table

DAX measures allow users to perform complex calculations and analysis on their data quickly and effectively, providing valuable insights into trends and patterns in their data.

Array functions

Many measures in Power BI desktop need to be array measure, because of the data model structure in a Power BI model.

One of the powerful features in DAX is this ability to create array measures, which are measures that return an array of values instead of a single value. This can also be complicated for users, who haven’t been working with data cubs.

Here are some examples of DAX array measures:

1. DISTINCTCOUNT function:

DISTINCTCOUNT returns the number of distinct values in a column or expression. When used in an array formula, it returns a list of distinct counts for each value in another column.

For example, the following DAX formula returns an array of distinct count values for the “Product” column:

= DISTINCTCOUNT(Products[Product])

2. FILTER function:

FILTER returns a table that meets certain criteria specified in the expression. When used in an array formula, it returns a list of filtered tables for each value in another column.

For example, the following DAX formula returns an array of filtered tables for each value in the “Category” column:

= FILTER(Products, Products[Category] = EARLIER(Products[Category]))

3. VALUES function:

VALUES returns a table of unique values in a column or expression. When used in an array formula, it returns a list of unique values for each value in another column.

For example, the following DAX formula returns an array of unique values for each value in the “Category” column:

= VALUES(Products[Category])

4. SUMX function:

SUMX returns the sum of an expression for each row of a table. When used in an array formula, it returns a list of sum values for each value in another column.

For example, the following DAX formula returns an array of sum values for each value in the “Product” column:

= SUMX(Products, Products[Price])

Array measures are a powerful way to analyse data and gain insights from multiple dimensions at once. With DAX, you can build complex array formulas that can handle large amounts of data and answer specific business questions.

1. Sum of Sales by Year:

SUMX(

GROUPBY(Sales, Sales[Year], “SalesByYear”, SUM(Sales[Amount])),

SalesByYear

)

This formula groups sales by year and calculates the sum of sales amount for each year.

2. Average Sales per Customer:

AVERAGEX(

GROUPBY(Sales, Sales[CustomerID], “SalesByCustomer”, SUM(Sales[Amount])),

SalesByCustomer

)

This formula groups sales by customer ID and calculates the sum of sales amount for each customer. Then, it takes the average of all the customer sales.

3. Last Sale Date for Each Customer:

MAXX(

GROUPBY(Sales, Sales[CustomerID], “LastSaleByCustomer”, MAX(Sales[Date])),

LastSaleByCustomer

)

This formula groups sales by customer ID and finds the maximum date of sales for each customer, which represents the last sale date for that customer.

4. Sales Growth Rate by Month:

DIVIDE(

SUM(Sales[Amount]),

CALCULATE(SUM(Sales[Amount]), DATEADD(Sales[Date], -1, MONTH)),

BLANK()

) – 1

This formula calculates the growth rate of sales from the previous month. It uses the DIVIDE function to divide the total sales amount by the total sales amount from the previous month. Then, it subtracts 1 to get the growth rate percentage.

5. Running Total of Sales by Month:

CALCULATE(

SUM(Sales[Amount]),

FILTER(

ALL(Sales),

Sales[Date] <= MAX(Sales[Date])

)

)

This formula calculates the running total of sales by month. It uses the CALCULATE function to add up the sales amount for all dates that are less than or equal to the maximum date in the current filter context. The FILTER function is used to remove any filters on the date column that might interfere with the running total calculation.

Time-intelligence functions

DAX (Data Analysis Expressions) Time-intelligence functions allow users to analyse data over time, providing insights into key trends and patterns.

Some common DAX Time-intelligence functions include:

  1. TOTALYTD: This function returns the total value of a given measure from the beginning of the year up to the selected date.
  2. TOTALQTD: This function returns the total value of a given measure from the beginning of the quarter up to the selected date.
  3. TOTALMTD: This function returns the total value of a given measure from the beginning of the month up to the selected date.
  4. SAMEPERIODLASTYEAR: This function returns the total value of a given measure for the same period last year.
  5. DATESYTD: This function generates a table with all the dates within the current year up to the selected date.
  6. DATEQTD: This function generates a table with all the dates within the current quarter up to the selected date.
  7. DATEADD: This function adds a specified number of units to a given date.

Example: DATEADD Returns a table that contains a column of dates, shifted either forward or backward in time by the specified number of intervals from the dates, in the current context.

Syntax: =DATEADD(<Dates>,<Number of intervals>,<Interval>)

where the <Interval> can either be Year, Month, Quarter or Day

Power BI – What is DAX?

These functions can be used in combination with other DAX functions to create more complex analyses and visualisations.

Conclusion

As we wrap up our exploration of DAX measures in Power BI, it’s clear that these tools are indispensable for any data professional. From calculating simple sums to performing complex time-based analyses, DAX measures empower us to make data-driven decisions with confidence.

We encourage you to experiment with the examples provided and discover the full potential of DAX in your own business scenarios. Remember, the power of data is at your fingertips, and with Power BI and DAX, there’s no limit to the insights you can uncover!

Further Reading

Power BI –Power Query M functions versus DAX

How Power BI Can Change Your Business

 

Categories
Data Visualisation Microsoft Power BI

Power BI – Dynamic Text for More Detailed Reports

Visualising data is crucial for decision making in any industry.

Power BI reports are a brilliantly efficient way to achieve this, as users have several useful visual options.

In this blog post you will learn how Power BI can display dynamic text for more detailed reports.

Power BI Dynamic Text

We will be using examples of sales data and HR data, but the logic, methods, and techniques can be used across all different industries, such as finance, healthcare, and retail and across all types of data sets.

DAX

To get the full benefit from this blog post, some experience and knowledge about DAX (Data Analysis eXpressions) is needed. We will use some DAX functions, but you could use a huge number of different DAX functions to create Dynamic text for more detailed reports.

Power BI Report Visuals

You have different options to provide your audience explanations or additional information to your Power BI report’s visuals. You can let Power BI desktop search for key influencers in your data model based on the visual by using the Artificial Intelligence Visual Smart Narrative.

Power BI Dynamic Text

You can write DAX measures and be in fully control of the text displayed in a text box (see the example below and the DAX used to achieve the example text).

Power BI Dynamic Text

Dyn text =

” The line chart above shows Profit for ” & VALUES ( Customers[Country] ) & ” over years by customer segment. The Sales growth for ”

& VALUES ( Customers[Country] ) & ” is ”

& FORMAT ( [Growth], “0.00” ) & “%, and the best selling product in ”

& VALUES ( Customers[Country] ) & ” is ”

& CONCATENATEX (

TOPN ( 1,  values(Items[Product Description]), [Sales],DESC ),

[Product Description],

“;”

) & “. At the moment we have ”

& COUNT ( Customers[Customer Id] ) & ” customers in ”

& VALUES ( Customers[Country] ) & ” and the average order sales is ”

& FORMAT ( DIVIDE ( [Sales], COUNTROWS ( Orders ), 0 ), “currency”, “en-gb” ) & “, and average quantity per order is ”

& FORMAT (

DIVIDE ( SUM ( Line_Items[Quantity] ), COUNTROWS ( orders ), 0 ),

“0,00”

) & ” units.”

 

The DAX in this example will take to account any filtering on the report page and change the dynamic text based on the filters.

You can use a combination of the two options, and you can also use the AI tool Q&A to generate your dynamic text boxes. Furthermore, you can create Custom Tooltips with dynamic text which will popup when your audience hover over visuals.

In Power BI desktop all dynamic text will be underlined (a blue line) and cannot be changed, but when the report has been published and shared, the audience will not see the blue lines.

Example 1 – Combination of Smart Narrative and your own DAX measures and text.

A line chart visual displaying sales over years and months has been selected and to get Power BI desktop to generate the text you will have to click “Narrative” on the Insert tab in the AI Visuals group.

You will get a text box where Power BI will write AI text based on the data set and selected visual.

Power BI Dynamic Text

You cannot control the AI text the Narrative tool writes, but to add your own text, just click inside the text box and type. The text “The total sales for all months” is entered here. After the added text, the total sales amount is going to be displayed.

Power BI Dynamic Text

When you click inside the text box a tool bar will appear. Click +VALUE and you can type in the DAX measure you want to display the result from. Here I want to display the result from a DAX measure called sales. I have formatted the result as currency, and I named the result “totsales.” The name can be used to display the result somewhere else in the text box.

Power BI Dynamic Text

In the same text box, you may want to explain to the audience, how the visual and the information have been filtered. In this example a slicer has been added to page to filter the data by country.

The following DAX measure will pick up if the column with the heading Country in the table “tblsales” is filtered. If it is, it will return the country name selected from the slicer. Otherwise, it will return the text string ” all countries”.

Filtered countries = if(ISFILTERED(tblsales[Country]),SELECTEDVALUE(tblsales[Country]),”all countries”)

Power BI Dynamic Text

If you look at the screenshot above, you can see that the total sales amount is added to the text box and a new line at the top has been typed:
“This page visuals and the information in this text box display data from”

Place the mouse course where you want to add the dynamic text and click “+Value” and again the options will appear.

Power BI Dynamic Text

Now the text will show this if the page has not been filtered by country:

Power BI Dynamic Text

And this if the page has been filtered by “UK”:

Power BI Dynamic Text

Example 2 – Combination of Q&A artificial intelligence and your own DAX measures and text.

If you do not want the smart narrative dynamic text, you can use the same method as in example 1 above. You will then just need to add a text box to the page.

In this example you will also see how the artificial intelligence tool Q&A can benefit you when you are creating dynamic text.

If we type “The percentage sales margin for” in a text box, then as in example 1 the DAX measure Filtered countries is used to return the country selected from the slicer. Then “is” is typed and then as in example 1, where a measure called Sales was added, another measure is added where the percentage sales margin was calculated. The same has been done to get the result from a profit measure.

At this point, the text box displays:

The percentage sales margin for Canada is 49.19 %,

the profit for Canada is £269,079,481,

and the total quantity for Canada is

 

You will not even always have to do measures to return calculated results in dynamic text. Here the artificial intelligence tool Q&A can be particularly useful.

To get the total quantity in the text box click “+Value.”

Power BI Dynamic Text

This will look in your data set and find a column with the name quantity and sum all the values in the column.

You will need to know your column headers.

Power BI Dynamic Text

“top 1 ClientName profit” Q&A will look in the column ClientName and find top 1 client’s value based on a measure called profit.

And the result will look like this:

Power BI Dynamic Text

And this:

Power BI Dynamic Text

Will return the name of top 1 client based on profit.

Power BI Dynamic Text

Example 3 – Dynamic text in custom Tooltips for more detailed reports.

The methods are the same as above! The only difference is that the text box needs to be created on a tooltip page.

Power BI Dynamic Text

The dynamic text is also filtered by the data point you hover over on the visual. In the screen shot above, the mouse courser is on top of the data point for August 2019.

Conclusion

You can add much more information to your reports by creating dynamic text boxes. They enable you to explain the visuals, you can also point out specific details, which are important for the audience.

Filtering can be confusing in a Power BI report for the audience, but as you saw in this blog post, you can explain filters in dynamic text.

Further Reading:

https://www.stl-training.co.uk/b/power-bi-artificial-intelligence-visuals-part-1-qa/