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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Employee
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
Employee
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
Employee
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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors