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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
jswartz_09
Frequent Visitor

Dax filter of all rows based on max value - SQL equivalent

Ok so currently I have a table with a multitude of columns and rows. What I want to do is filter all the rows based on the maximum value of one column OrderID for the CustomerID. 

 

Table looks like This:

CustomerIDCustNameOrderIDOrderDateOrderStatusOrderReceivedDate

1

John111401/01/2024Complete01/08/2024
1John111501/10/2024Complete01/15/2024
2Dan221203/05/2024Shipped 
2Dan345703/06/2024Shipped 
2Dan487503/08/2024Shipped 
3Bob111212/17/2023Complete12/26/2023
3Bob555503/10/2024Pending 
4Frank111012/10/2023Complete12/15/2023
4Frank675803/18/2024Pending 
4Frank675903/18/2024Shipped 

 

 

Basically in SQL what I am trying to accomplish would be written like the following

 

SELECT *

FROM myTable a1

where a1.OrderID = (select max(b1.OrderID)

                                from myTable b1

                                where b1.CustomerID = a1.CustomerID)

 

Output would be:

 

CustomerIDCustNameOrderIDOrderDateOrderStatusOrderReceivedDate
1John111501/10/2024Complete01/15/2024
2Dan487503/08/2024Shipped 
3Bob555503/10/2024Pending 
4Frank675903/18/2024Shipped 

 

Currently I can achieve this by the following Dax measure: 

isMax = IF(myTable[OrderID] = CALCULATE(MAX(myTable[OrderID],AllEXCEPT(myTable[CustomerID])), 1, 0)

then I filter that measure to 1. 

 

I feel like there has to be another way to do this right? Maybe creating a new table to do this? 

Thank you

14 REPLIES 14
Anonymous
Not applicable

Hi @jswartz_09 ,

 

Have a good day. Has your problem been solved?

 

Best Regards,

Wearsky

jswartz_09
Frequent Visitor

Now now don't you two fight over my question ;). These are excellent solutions. @SachinNandanwar  I was wondering if you could explain how your solution works in more detail. I'm trying to learn more Dax.

SachinNandanwar
Super User
Super User

How about this ?

Customer_Measure_Id =
VAR _OrderId =
    MAX ( 'Tbl'[OrderID] )
VAR _MaxOrder =
    SUMMARIZE (
        ALL ( 'Tbl' ),
        'Tbl'[CustomerID],
        "OrderId", MAX ( 'Tbl'[OrderID] )
    )
VAR FilterVar =
    FILTER ( _MaxOrder, [OrderId] = _OrderId )
RETURN
    MAXX ( FilterVar, 'Tbl'[CustomerID] )

 

SachinNandanwar_0-1726269592326.png
Just replace CustomerId column with Customer_Measure_Id  in the display. 



Regards,
Sachin
Check out my Blog

Multiple red flags.

 

https://www.sqlbi.com/articles/all-the-secrets-of-summarize/

 

Again - use DAX Studio to measure for yourself. No need to guess.

Thats interesting.Thanks for pointing that out.

 

the article you quoted doesnt necessarily states that using SUMMARIZE are red flags but explains the limitations of using it wrt to extended columns and suggests usage of ADDCOLUMNS with CALCULATE.

 

I found a better explaination here.

 

https://www.sqlbi.com/articles/best-practices-using-summarize-and-addcolumns

 

The best practice proposed is to use

 

ADDCOLUMNS(
SUMMARIZE( <table>, <group by column> ),
<column_name>, CALCULATE( <expression> )
)

 

which I agree. But in my solution the change isnt working. I changed the DAX from this

 

VAR _MaxOrder = SUMMARIZE (
ALL ( Tbl ),
'Tbl'[CustomerID],
"Order_Id", ( MAX ( 'Tbl'[OrderID] ) )
)

 

to this

 

VAR _MaxOrder = ADDCOLUMNS (
SUMMARIZE ( ALL ( Tbl ), 'Tbl'[CustomerID] ),
"Order_Id", CALCULATE ( MAX ( 'Tbl'[OrderID] ) )
)

 

and it doesnt work.Any thoughts ?

 

 

Recording 2024-09-15 at 01.16.43.gif

 



Regards,
Sachin
Check out my Blog

@SachinNandanwar  Not sure what your question is.  Maybe open a new thread?  

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Its related to the issue in this thread.



Regards,
Sachin
Check out my Blog
lbendlin
Super User
Super User

Since this data is immutable you can create a calculated column, or do this in Power Query.  (or even in SQL if you want)

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZDBCsIwEER/RXoWurtpmnhV8eBJ8Fh6qDTYoqZF/H/csNHSWtEQyO7whmFSFAkmy2TfNZ4fRMz4AUz5ElBYNt2tv7qHi7oVvVxOjVoAhHkj6sFILG2r4CPCsIBKQb98x6bte1fztJjQKtMm0vlvOrNGR9p+oxVP6+4kDYIVKUUTaDVuwDrloo99mo+kDM0PztetP79Tgra7V/4iORBzYD5Hfkp9OHOjbUyy/yQxv5ryo/7lEw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CustomerID = _t, CustName = _t, OrderID = _t, OrderDate = _t, OrderStatus = _t, OrderReceivedDate = _t]),
    #"Grouped Rows" = Table.Group(Source, {"CustomerID"}, {{"Rows", each _, type table [CustomerID=nullable text, CustName=nullable text, OrderID=nullable text, OrderDate=nullable text, OrderStatus=nullable text, OrderReceivedDate=nullable text]}, {"Max Order", each List.Max([OrderID]), type nullable text}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"CustName", "OrderID", "OrderDate", "OrderStatus", "OrderReceivedDate"}, {"CustName", "OrderID", "OrderDate", "OrderStatus", "OrderReceivedDate"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Rows", each ([OrderID] = [Max Order])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Max Order"})
in
    #"Removed Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

This is nice for power query, but how would you do this in Dax?

A calculated column that you can use for filtering:

 

lbendlin_0-1726264234986.png

 

Yeah that's what I've been doing. Sorry I'm being difficult but is there another way? Or better way?

What do you not like about this way?

Nothing I don't like. Just wondering if there are other ways

There are, but most likely they will perform worse  (depends on the size of your table too).

 

If in doubt - use DAX Studio to measure query performance and examine the query plan.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.