Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Don'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.

Reply
mbahonen
Helper III
Helper III

Simplifying a SUMMARIZECOLUMNS with FILTERS EVALUATE query

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. 

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

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] )
    )

 

  • SUMMARIZECOLUMNS itself can add columns computed in the filter context of the Groupby columns, so there is no need to wrap SUMMARIZECOLUMNS in ADDCOLUMNS in this case.
  • Also, for clarity, I suggest creating variables for the filters.
  • It's generally best to filter columns rather than tables, which is why I suggest splitting the filters by column. See https://www.sqlbi.com/articles/filter-columns-not-tables-in-dax/

Does the above query work as intended?

 

Suggested reading:

https://www.sqlbi.com/articles/introducing-summarizecolumns/

https://dax.guide/summarizecolumns/

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

5 REPLIES 5
OwenAuger
Super User
Super User

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] )
    )

 

  • SUMMARIZECOLUMNS itself can add columns computed in the filter context of the Groupby columns, so there is no need to wrap SUMMARIZECOLUMNS in ADDCOLUMNS in this case.
  • Also, for clarity, I suggest creating variables for the filters.
  • It's generally best to filter columns rather than tables, which is why I suggest splitting the filters by column. See https://www.sqlbi.com/articles/filter-columns-not-tables-in-dax/

Does the above query work as intended?

 

Suggested reading:

https://www.sqlbi.com/articles/introducing-summarizecolumns/

https://dax.guide/summarizecolumns/

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

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


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

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!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.