Forum Discussion

bullius's avatar
bullius
Helper V
8 years ago
Solved

EARLIER function in M/Query Editor

Hi

 

I have data that looks like this:

 

IDName
1Apple
1Banana
2Orange
3Pear
4Pineapple
4Grape
4Mango
5Peach

 

I want it to look like this:

 

IDName1Name2Name3
1AppleBanana 
2Orange  
3Pear  
4PineappleGrapeMango
5Peach  

 

I plan to do this by ranking the names per ID, e.g.

 

IDNameRank
1Apple1
1Banana2
2Orange1
3Pear1
4Pineapple1
4Grape2
4Mango3
5Peach1

 

...then pivoting the table using the rank field. I don't know if this will work, but to do it I need to use something like the EARLIER function in DAX. Anyone know an alternatinve in M? (Or a better way to achieve my end goal).

 

Thanks!

  • HI bullius,

     

    Yes it is possible to do that in M.

    To sum up group by your id and add an index.

    You can add this code into your steps: 

    #"Grouped Rows" = Table.Group(#"Promoted Headers", {"ID"}, {{"Count", each  Table.AddIndexColumn(_, "Index", 1, 1), type table}})

     

    Hope it helps...

     

    Ninter 

9 Replies

  • prateekraina's avatar
    prateekraina
    Memorable Member

    Hi bullius,

     

    You are absolutely right. You can rank and then pivot the column to achieve what you need.
    Here is a sample pbix which showcases this.

     

    Prateek Raina

    • bullius's avatar
      bullius
      Helper V

      Thanks prateekraina. My actual dataset is much larger than the example and does not include a Rank field. My question is how do I create one? I would use the EARLIER function in DAX to make sure the ranking restarts with each new ID, but can't find an alternative in M.

      • Interkoubess's avatar
        Interkoubess
        Solution Sage

        HI bullius,

         

        Yes it is possible to do that in M.

        To sum up group by your id and add an index.

        You can add this code into your steps: 

        #"Grouped Rows" = Table.Group(#"Promoted Headers", {"ID"}, {{"Count", each  Table.AddIndexColumn(_, "Index", 1, 1), type table}})

         

        Hope it helps...

         

        Ninter 

    • alfranco17's avatar
      alfranco17
      Advocate I

      Hi Patreek.

       

      I have the same question. If you have it at hand, would you please repost your pbix?

       

      Thank you.

      Armando.