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
Cogidubnus_Rex
Frequent Visitor

Sort by two columns in DAX

I've spent hours with one of my Power BI consultants trying to do in DAX what is a two-second operation in Query Editor, but unfortunately this isn't quite happening!

 

All we want to do is to sort low to high value within the categories of another column.  Unfortunately this has to be in DAX.

 

Below is a simplified Excel table to illustrate:

 

Due statusValueInvoiceCumulative # invoicesOn timeCumulative On time # invoicesCumulative on-time %Rank
Past due2011000%1
Past due2612000%2
Past due5213000%3
Past due42014000%4
Due13151120%5
Due52161233%6
Due249171343%7
Due953181450%8
Due220854191556%9
Due2349237531101660%10
Not yet due41111764%11
Not yet due71121867%12
Not yet due241131969%13
Not yet due74411411071%14
Not yet due454511511173%15
Not yet due2457711611275%16
Not yet due567547411711376%17
Not yet due56756756711811478%18

 

I don't understand why asceding ordering by value and then by due status doesn't give the right order (it seems to shuffle the invoices around), but getting the right answer here seems to be very complicated, and all other examples in the community seem to do something different.

 

Ultimately we then want to add the 'On-time %' calculation (as a measure, to be most efficient) but hopefully the above is clear.

 

Thank you in advance to the superstars who help us simple folk!

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @Cogidubnus_Rex 

In Edit queries,

Add conditional column

sort the "status rank" column first, then sort the "value" column, add an index column,

Capture4.JPG

Capture5.JPG

Close &&apply, create columns

 

clc invoice = CALCULATE(SUM('Table'[Invoice]),FILTER('Table','Table'[Index]<=EARLIER('Table'[Index])))

on time = IF([Due status]="Past due",0,1)

clc on time invo = IF([on time]=0,0,CALCULATE(SUM('Table'[Invoice]),FILTER('Table','Table'[Index]<=EARLIER('Table'[Index])&&[on time]=1)))

clc on time% = [clc on time invo]/[clc invoice]

 

Capture7.JPG

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-juanli-msft
Community Support
Community Support

Hi @Cogidubnus_Rex 

In Edit queries,

Add conditional column

sort the "status rank" column first, then sort the "value" column, add an index column,

Capture4.JPG

Capture5.JPG

Close &&apply, create columns

 

clc invoice = CALCULATE(SUM('Table'[Invoice]),FILTER('Table','Table'[Index]<=EARLIER('Table'[Index])))

on time = IF([Due status]="Past due",0,1)

clc on time invo = IF([on time]=0,0,CALCULATE(SUM('Table'[Invoice]),FILTER('Table','Table'[Index]<=EARLIER('Table'[Index])&&[on time]=1)))

clc on time% = [clc on time invo]/[clc invoice]

 

Capture7.JPG

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Maggie, thank you so much for your solution, it's beautiful.  I think I've fallen in love...

Mariusz
Community Champion
Community Champion

Hi @Cogidubnus_Rex 

 

Witch one of the columns in your sample are you trying to recreate?

 

Thanks

Mariusz

 

 

Thanks for the question.

The 'Due status' column must be ordered first by 'Past due', then 'Due', then 'Not yet due', but the 'Value' column needs to show from low value to high value within each of those 'Due status' subsets.

 

Hopefully that then aligns with what is in the table.

 

Once the invoices are ordered correctly, which is what I haven't been able to do, I can add a rank (1, 2, 3, etc.) that will enable the user to correctly order the invoices easily, before then going on to show what % of invoices will be paid on-time.

 

Some of the above is for context (unless people want to jump ahead and give a solution to everything), but my question is simply to order the invoices by 'Value' within each 'Due status', which must also be in order.

 

I hope that clarifies everything.

 

Thanks once again.

Hi @Cogidubnus_Rex 

 

Please see the attached file with the solution. 

To sort Due Status I've added an extra dimension with dueStatusID that is used for sorting in the measure so it will have to be recreated in your model.

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.



Check out Page 2, Table 3 and see if that is what you want.

 

I added a column called Due Sort that is used as a Sort By column. You should be able to add that as a column in your query very easily.

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks, Greg.

 

I'm not quite sure I was clear enough before, therefore, as the table I provided was just for illustrative purposes.  The real data has 20k rows, so what I'm after is the DAX to order the rows correctly.

 

Or am I missing something from the file you shared?

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.