Forum Discussion
bullius
8 years agoHelper V
EARLIER function in M/Query Editor
Hi I have data that looks like this: ID Name 1 Apple 1 Banana 2 Orange 3 Pear 4 Pineapple 4 Grape 4 Mango 5 Peach I want it to look like this: ...
- 8 years ago
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
7 years agoAdvocate I
Hi Patreek.
I have the same question. If you have it at hand, would you please repost your pbix?
Thank you.
Armando.
Ashish_Mathur
7 years agoSuper User
Hi,
Share some data and show the expected result.
- alfranco177 years agoAdvocate I
Thanks for your reply, Ashish.
This is what I have:
Name Date Alice 2019-01-01 Alice 2019-01-03 Alice 2019-01-14 John 2019-01-03 John 2019-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 SourceAnd here is what I need. I want to know the number of the visit by date.
Name Date Visit Alice 2019-01-01 1 Alice 2019-01-03 2 Alice 2019-01-14 3 John 2019-01-03 1 John 2019-01-12 2 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.
- alfranco177 years agoAdvocate I
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