In this article I have presented some of the frequent DAX queries I was encountering in my day to day work. The objective of this article is to help users with DAX through an example based approach. I found that it is easier to comprehend the nuances of the DAX language if it supported by simple examples. Note — it is not strictly neccessary that the results have to be obtained via DAX only. If the data model is good then Power BI
visuals can often meet the requirements. In this article I have covered the following DAX expressions: Sample dataThe DAX expressions in this article are written around the MS Access sample database downloadable from Microsoft Learning. A copy of the same can also be downloaded from my Github repo here. A copy of the Power BI report which references this MS Access database can be downloaded from my Github repo here. Database schemaPower BI modelDataTo get a feel of what the data looks like I have presented the top 5 rows from each of the tables in this database Top N rows from all tablesDAX studio primerHow to use DAX studio?DAX studio from Microsoft is a very handy tool if you want to experiment with DAX queries outside of Power BI. I have listed some informative videos below. DAX Studio runs independently of Power BI, however it expects a running instance of Power BI to establish a connection.
How to execute Table expressions?DAX studio expects any table expression to be encapsulated inside a EVALUATE How to execute Scalar expressions?To execute any expression that returns a scalar value (i.e. not a table) encapsulate the
expression in a EVALUATE How to create a measure (MEASURE,SUM)?In this example we are calculating the total sales per manufacturer. When using DAX studio, the DEFINE How to create a calculated column(UPPER,COLUMN)?In the following example we are creating a new column which converts the manufacturer name to upper case DEFINE List of unique Product Segments (DISTINCT, ORDER BY)In this example we are displaying an unique list of product segments. EVALUATE Use the EVALUATE Distinct list of financial years from the Sales table (DISTINCT)In this query we are creating a calculated column to get the year component from the sales transaction date and then using the DEFINE Distinct list of financial years from the Sales table(VALUES)The DEFINE Count of rows from all the tables (ROW,UNION)This helps towards the answering the question — “How much data does my dataset hold?” EVALUATE In the following example we have added an EVALUATE Display N rows from a table (TOPN)Use this when you want to do a quick visual inspection of a table. EVALUATE The EVALUATE Find rows with blank column values (COUNTBLANK, FILTER,COUNTROWS)This answers the question. How many rows in the bi_geo table do not have a Region value? EVALUATE The same result can also be achieved by using EVALUATE In the following example we are counting blank regions for a specific country EVALUATE In the following example we are displaying all rows where Region is non-blank EVALUATE Add a calculated column to return 1 if region is blank otherwise 0 (ISBLANK, IF)In this example we are creating a new calculated column on the table EVALUATE What is the distribution of values in the Country column of the bi_geo table? (SUMMARIZE)In this example we want to know the distinct list of countries and the total number of rows per country EVALUATE What is the distribution of values in the Region columm of the bi_geo table? (SUMMARIZE,SUMMARIZECOLUMNS,GROUPBY)This verifies
that total rows(99618)=total non blanks(18929+14512+6507) + total blanks(59670). Note the presence of the blank row and the value of the Example using SUMMARIZE EVALUATE Example using SUMMARIZECOLUMNS EVALUATE Example using GROUPBY EVALUATE What is the distribution of values in the Region columm of the bi_geo table taking into account the blank values? (GROUPBY, SELECTEDGROUP(),IF, ISBLANK)Approach 1 In this approach we are using EVALUATE Approach 2 In this approach we are first creating a calculated table with a new column DEFINE Approach 3 This is similar to the previous approach where we first created a calculated table using DEFINE Approach 4 We are expanding on the previous approach of using DEFINE Approach 5 We could simply use EVALUATE Are there any duplicates in the ‘zip’ column of bi_Geo table? (SUMMARIZE,COUNT)This will help us establish the cardinality of a foreign key relationship with the EVALUATE The above can also be achieved by using EVALUATE What is the highest number of times a single ‘zip’ code has been duplicated? (SUMMARIZECOLUMNS)In this example EVALUATE
How many values in the ‘zip’ column are not duplicated? (SUMMARIZECOLUMNS, FILTER, COUNT)We will arrive at this result in 2 steps. We will first Step 1: Use EVALUATE Step 2:Use EVALUATE Total units sold and total revenue earned per Product Segment (SUMMARIZE, SUM, ROUND)EVALUATE Min,Max,Avg sales per Product Segment (GROUPBY, SUMX,MINX, MAXX, AVERAGEX)In this example we are calculating the statistics of sales in bi_SalesFact table on a per segment basis EVALUATE Total units sold and revenue earned per Manufacturer (SELECTCOLUMNS)EVALUATE Total units sold and revenue earned per Manufacturer (SUMMARIZE)We are using EVALUATE Sort the manufacturers on Total units sold (SELECTCOLUMNS, ORDER BY)EVALUATE The above can also be achieved by using SUMMARIZE EVALUATE
Total units sold and revenue earned per Manufacturer per SegmentDEFINE Total units sold and revenue earned per Manufacturer per Segment (renamed columns)In this example we demonstrate how to rename the columns DEFINE Best selling and worst selling Product segment for every Manufacturer (SELECTEDVALUE,SUMMARIZE, MAXX, SUM, MINX)We will attempt to answer the question — “For every manufacturer what was the best performing and worst performing product segment with regards to units sold?” To achieve this we will create 4 measures
Step 1 Use the DEFINE MEASURE bi_manufacturer[segment_maxunits_value] = ConclusionI would be delighted to hear from you. Did you spot any mistakes? Did I miss anything obvious? Your feedback would be very beneficial for my future work. Thank you. What is the best way to learn DAX for Power BI?The best way to learn DAX is to create some basic formulas, use it with some actual data, and see the results for yourself. The examples and tasks here use the Contoso Sample DAX Formulas.
Is Power BI hard to learn DAX?Almost anyone can learn DAX with enough time and effort. The secret to mastering DAX is learning the theory first. The formula is simple, but the process is slow. Begin by studying how DAX views the underlying data models.
How do you remember DAX functions?Remember: DAX formulas always start with an equal sign (=). You can provide any expression that evaluates to a scalar, or an expression that can be converted to a scalar after the equals sign. Month-to-date (MTD): a period starting at the beginning of the current calendar month and ending at the current date.
How do you use DAX in Power BI?DAX Calculation Types. Go to the Modeling tab in Power BI Desktop.. Then select New Column option. A Formula bar will open showing “Column =”. You can replace the “Column” word with the column name you want.. After this, enter the expression for the calculated column on the right of the equals to sign.. |