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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Yucy
Helper I
Helper I

Need help on a DAX Formula

Thanks so much for any help. I have a table, I need to use filter dax, but never working for me

 

Attached is a screenshot, I need to keep the rows highlighted in yellow. Generally speaking, if number is unique, keep the row, if the number in not unique, keep the row of rank at 2

 

Thanksquestion dax.gif

NumberAmended Opened DatetimeResolvedRankCase Duration
CS00752491/02/2020 8:001/02/2020 9:4826488
CS00752491/02/2020 8:001/10/2020 0:001194400
CS00752501/02/2020 8:001/10/2020 0:001194400
CS00752701/02/2020 8:121/02/2020 11:46312836
CS00752701/02/2020 8:121/03/2020 8:46234458
CS00752701/02/2020 8:121/11/2020 0:001226052
CS00772041/08/2020 10:261/08/2020 11:2043236
CS00772041/08/2020 10:261/09/2020 9:00327273
CS00772041/08/2020 10:261/10/2020 8:16256986
CS00772041/08/2020 10:261/18/2020 0:011250417
CS00772051/08/2020 10:251/08/2020 10:26171
1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

This was a little trickier in DAX than I expected, but here is one way to do it.

 

Make a Calculated Table with this expression:
Filtered = FILTER(Ranks, Ranks[Rank]=CALCULATE(CALCULATE(MAX(Ranks[Rank]), All(Ranks), VALUES(Ranks[Number]), Ranks[Rank] <=2)))
 
If you didn't need a separate DAX table, you could use the same Filter() expression in a Calculate() if needed.  Note that the nested Calculate()s were necessary to get context transition to kick in.
 
If this works, please mark it as solution.  Kudos are appreciated too.
 
Regards,
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

5 REPLIES 5
camargos88
Community Champion
Community Champion

@Yucy ,

 

In addition to @mahoneypat  answers, you can create a calculated column on the same table:

 

Filter =
VAR _qt = CALCULATE(COUNTROWS('Table'); ALLEXCEPT('Table'; 'Table'[Number]))
RETURN IF(_qt = 1; 1; IF(_qt > 1 && 'Table'[Rank] = 2; 1; 0))
 
Ricardo


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



it is not working for me, not sure why

mahoneypat
Microsoft Employee
Microsoft Employee

This was a little trickier in DAX than I expected, but here is one way to do it.

 

Make a Calculated Table with this expression:
Filtered = FILTER(Ranks, Ranks[Rank]=CALCULATE(CALCULATE(MAX(Ranks[Rank]), All(Ranks), VALUES(Ranks[Number]), Ranks[Rank] <=2)))
 
If you didn't need a separate DAX table, you could use the same Filter() expression in a Calculate() if needed.  Note that the nested Calculate()s were necessary to get context transition to kick in.
 
If this works, please mark it as solution.  Kudos are appreciated too.
 
Regards,
Pat
 




Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat Thank you

mahoneypat
Microsoft Employee
Microsoft Employee

If you don't need the unhighlighted rows in your model, I would do this in M/Query Editor as below (paste it into a blank query in Advanced Editor).  It can also be done in DAX and I may post that next.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZFBDsMgDAS/UnGOFHuxwXDtE3qM8v9vFJLQhkZqkXrwgREj78KyuPuDKCokucnxTJhBoJtlog6kLFYAygQxc+v0U2XaAR2gThIph5Os9IccP2VGB5izhEJ8pTAfhmTfwOZW6EXURlzmS2ogkOItR5Dssh0hKSP0hDOqXu95nFN/k1P7qG11rYyI6Ifk9tqlSOusIdnYZrZXaW6llYRjZ+vFvpJw6JHduj4B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Number = _t, #"Amended Opened Datetime" = _t, Resolved = _t, Rank = _t, #"Case Duration" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Number", type text}, {"Amended Opened Datetime", type datetime}, {"Resolved", type text}, {"Rank", Int64.Type}, {"Case Duration", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Number"}, {{"Count", each Table.RowCount(_), type number}, {"AllRows", each _, type table [Number=text, Amended Opened Datetime=datetime, Resolved=text, Rank=number, Case Duration=number]}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"Amended Opened Datetime", "Resolved", "Rank", "Case Duration"}, {"Amended Opened Datetime", "Resolved", "Rank", "Case Duration"}),
#"Added Custom" = Table.AddColumn(#"Expanded AllRows", "Keep", each if [Count]=1 then "Y" else if [Rank]= 2 then "Y" else "N"),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Keep] = "Y")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Keep", "Count"})
in
#"Removed Columns"

 

If this meets your need, please mark this as the solution. 

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.