Forum Discussion
Month MAX record
- 9 years ago
An alternative in - advanced - Power Query would be:
let Source = TransactionTable, Grouped = Table.Group(Source, {"Tran_DT"}, {{"AllData", each Table.MaxN(_,"TransactionCount",1), Value.Type(Source)}},null, (x,y) => let Sort1 = Value.Compare(Date.Year(x[Tran_DT]),Date.Year(y[Tran_DT])), Sort2 = if Sort1 <> 0 then Sort1 else Value.Compare(Date.Month(x[Tran_DT]),Date.Month(y[Tran_DT])) in Sort2), Removed = Table.RemoveColumns(Grouped,{"Tran_DT"}), Expanded = Table.ExpandTableColumn(Removed, "AllData", {"Tran_DT", "TransactionCount"}, {"Tran_DT", "TransactionCount"}) in ExpandedBasically it is "Group By" Tran_DT, but taking full advantage of Table.Group capacities;
Syntax of Table.Group:
Table.Group(table as table, key as any, aggregatedColumns as list, optional groupKind as nullable number, optional comparer as function) as table
A custom comparer function is provided to group on year/month (the inner "let .. in" part).
Parameter aggregatedColumns is adjusted so a table is returned with 1 record (the maximum TransactionCount) and with the same columntypes as table Source (this is the part Value.Type(Source)).
Note that the column "Tran_DT" after grouping does not contain the date on which the maximum was reached, so this column is removed.
The nested tables in column "AllData" contain exactly the information you require, so after expanding the nested tables, you have your result.
vanessafvg my calender table is mapped with sales data.
ok so if your date table is mapped from date to transaction date
then something like this should work
measure = CALCULATE('Table'[TransactionCount], LASTDATE(Dates[Date]))
then drag your year / month on to the visual you want to see and it should bring back the data for the last day
- anupampandey9 years ago
Helper III
It is not working.
By the way what will LASTDATE function do? I need on which day max transaction happened in every month.
- vanessafvg9 years ago
Community Champion
anupampandey if you say its not working you need to give me more information.
firstly please post the code you created
then screen shot how you were viewing it detailing the issue
you can read more about last date here, https://msdn.microsoft.com/en-us/library/ee634380.aspx
when you say its not working, what is not working? please provide me with details, i can't help you solve your issue if you dont give me information to work with
- anupampandey9 years ago
Helper III
vanessafvg I am using below code.
C1 = CALCULATE(MAX('Z test1'[CountTran]),LASTDATE(Calender[DateKey]))
I have got the result as explained by you.
This result is count of transactions done on lastdate of all the months during the year. I am looking for the maximum transactions done in those months and date pertains to that max count transaction.
Below is the image: left side is the sample data and right side is my desired output for which I am looking for solution.
Sample
Also above sample is summarised one. Actually I have put this on my sales fact table which is having 2 million records where each date is getting repeated. One any particular date there are thousands of transactions which first need to calculated date wise and than taking out the max.
Thanks & Regards,
Anupam