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
bullius
Helper V
Helper V

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!

1 ACCEPTED SOLUTION

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 

View solution in original post

9 REPLIES 9
prateekraina
Memorable Member
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

Hi Patreek.

 

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

 

Thank you.

Armando.

Hi,

Share some data and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks for your reply, Ashish.

 

This is what I have:

NameDate
Alice2019-01-01
Alice2019-01-03
Alice2019-01-14
John2019-01-03
John2019-01-12

 

let
    Source = #table( type table[Name=text, #"Date"=date], { {"Alice",#date(2019,1,1)},{"Alice",#date(2019,1,3)}, {"Alice",#date(2019,1,14)}, {"John",#date(2019,1,12)}, {"John",#date(2019,1,3)} } )
in
    Source

And here is what I need. I want to know the number of the visit by date.

NameDateVisit
Alice2019-01-011
Alice2019-01-032
Alice2019-01-143
John2019-01-031
John2019-01-122

 

I was able to get this using DAX, but I would like to have it there in the query.

Visit =
CALCULATE (
    COUNTROWS ( 'Table' ),
    FILTER (
        ALL ( 'Table' ),
        'Table'[Name] = EARLIER ( 'Table'[Name] )
            && 'Table'[Date] <= EARLIER ( 'Table'[Date] )
    )
)

Thanks.

Armando.

Found a solution. I could not create it from scratch, but here it is. Is there an easier way?

let
    Source = #table( type table[Name=text, #"Date"=date], { {"Alice",#date(2019,1,1)},{"Alice",#date(2019,1,3)}, {"Alice",#date(2019,1,14)}, {"John",#date(2019,1,12)}, {"John",#date(2019,1,3)} } ),
    Table1 = Table.TransformColumnTypes(Source,{{"Date", type text}}),
    AddCount = Table.AddColumn(
              Table1, 
              "Visit", //(a) is a parameter for function, which equals current record, and function should return value for new cell of "SubcategoryRanking"
              (a)=> Table.RowCount( 
                        Table.SelectRows( 
                        Table1, //(b) equals whole table1. This function returns table filtered by given criteria
                        (b) =>  b[Name] = a[Name] and b[Date] < a[Date]) 
                    ) + 1,
              Int64.Type),
    #"Sorted Rows" = Table.Sort(AddCount,{{"Name", Order.Ascending}, {"Visit", Order.Ascending}})
in
    #"Sorted Rows"

Thanks!

Armando

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.

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 

Thanks! That works.

Hi @bullius,

 

@Interkoubess is absolutely right. I haved added his step in the same pbix.

 

Prateek Raina

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.