Forum Discussion

Cogidubnus_Rex's avatar
Cogidubnus_Rex
Frequent Visitor
6 years ago
Solved

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!

  • Hi Cogidubnus_Rex 

    In Edit queries,

    Add conditional column

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

    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]

     

    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.

7 Replies

  • v-juanli-msft's avatar
    v-juanli-msft
    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,

    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]

     

    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.

    • Cogidubnus_Rex's avatar
      Cogidubnus_Rex
      Frequent Visitor

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

  • Mariusz's avatar
    Mariusz
    Community Champion

    Hi Cogidubnus_Rex 

     

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

     

    Thanks

    Mariusz

     

     

    • Cogidubnus_Rex's avatar
      Cogidubnus_Rex
      Frequent Visitor

      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.

      • Greg_Deckler's avatar
        Greg_Deckler
        Community Champion

        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.