Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
I'm trying to simplify a DAX Query formula and hoping someone can help me. I'm still somewhat new to DAX Queries to extract data from my model and am not always sure of the syntax. I created the Sample query below to illustrate what I have created. This works and does what I want it to do, which is to give me a total value for all dates (within the defined date range) for an ID (there are many, so this is my high level group). I just want one total though and don't want to list rows of records by the date or other dimensions. As I said, the below formula seems to be doing what I want, but I have to duplicate the FILTER values in both the SUMMARIZECOLUMNS portion AND in the ADDCOLUMNS portion for my SUM value. Is there any way to simplify this formula so I only need to apply the filters one time?
EVALUATE
ADDCOLUMNS (
SUMMARIZECOLUMNS (
'FactTable'[ID],
FILTER (
'FactTable',
'FactTable'[ID] IN { "000012345678" }
&& 'FactTable'[Date] >= DATE ( 2024, 6, 30 )
&& 'FactTable'[Date] <= DATE ( 2024, 11, 30 )
&& 'FactTable'[OtherDimension] = "True"
)
),
"TotalToSum",
CALCULATE (
SUM ( 'FactTable'[TotalToSum] ),
FILTER (
'FactTable',
'FactTable'[ID] IN { "000012345678" }
&& 'FactTable'[Date] >= DATE ( 2024, 6, 30 )
&& 'FactTable'[Date] <= DATE ( 2024, 11, 30 )
&& 'FactTable'[OtherDimension] = "True"
)
)
)
ETA: I just realized this formula doesn't do exactly as I want either. When I remove the ID filter, it then lists all the IDs with the same total rather than grouping the total by the ID which is what I'm trying to do.
Solved! Go to Solution.
Hi @mbahonen
I would recommend something like this:
EVALUATE
VAR Filter_ID =
TREATAS ( { "000012345678" }, 'FactTable'[ID] )
VAR Filter_Date =
DATESBETWEEN ( 'FactTable'[Date], DATE ( 2024, 6, 30 ), DATE ( 2024, 11, 30 ) )
VAR Filter_OtherDimension =
TREATAS ( { "True" }, 'FactTable'[OtherDimension] )
RETURN
SUMMARIZECOLUMNS (
-- Groupby columns
'FactTable'[ID],
-- Filters
Filter_ID,
Filter_Date,
Filter_OtherDimension,
-- Additional columns
"TotalToSum", SUM ( 'FactTable'[TotalToSum] )
)
Does the above query work as intended?
Suggested reading:
https://www.sqlbi.com/articles/introducing-summarizecolumns/
https://dax.guide/summarizecolumns/
Regards
Hi @mbahonen
I would recommend something like this:
EVALUATE
VAR Filter_ID =
TREATAS ( { "000012345678" }, 'FactTable'[ID] )
VAR Filter_Date =
DATESBETWEEN ( 'FactTable'[Date], DATE ( 2024, 6, 30 ), DATE ( 2024, 11, 30 ) )
VAR Filter_OtherDimension =
TREATAS ( { "True" }, 'FactTable'[OtherDimension] )
RETURN
SUMMARIZECOLUMNS (
-- Groupby columns
'FactTable'[ID],
-- Filters
Filter_ID,
Filter_Date,
Filter_OtherDimension,
-- Additional columns
"TotalToSum", SUM ( 'FactTable'[TotalToSum] )
)
Does the above query work as intended?
Suggested reading:
https://www.sqlbi.com/articles/introducing-summarizecolumns/
https://dax.guide/summarizecolumns/
Regards
So one question I have is this... I used the ADDCOLUMNS function in my original version on the advisement of Marco from SQLBI via this article: https://www.sqlbi.com/articles/all-the-secrets-of-summarize/
Do you have any concerns with your version without using it?
No concerns 🙂
The SUMMARIZECOLUMNS function is designed and optimized to handle computed columns like this. I would consider it a standard summarization function in any DAX query. Indeed, Power BI visuals generate DAX queries with this kind of syntax.
Most examples on https://dax.guide/ use a similar pattern to combine grouping columns and computed columns.
Also, until recently, SUMMARIZECOLUMNS couldn't be used reliably within ADDCOLUMNS among other scenarios.
(Some discusson here)
With the older function SUMMARIZE, ADDCOLUMNS is best practice due to some quirks mentioned in the article you linked to.
Regards
Thanks again for that extra detail! And for what its worth, so far all my versions of the adapted query have worked as I am expecting!
Thank you! That works great! Thank you also for inlcuding the comments with the sections. Its not always easy to see where the pieces fit in from the official documentation. I appreciate your help!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
13 | |
11 | |
10 | |
7 | |
6 |
User | Count |
---|---|
13 | |
12 | |
12 | |
8 | |
8 |