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 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
i understand the requirements, i dont understand why what i gave you isnt working, i want you to show me the results of the dax code i gave you
anupampandey please provide the results so i can see what is going on
- Okechukwu_Ossai9 years agoNew Member
@vanessafvg I guess the concept of max transaction date here is different from lastdate.
I believe it refers to the date where maximum or largest transaction occurred.
- MarcelBeug9 years ago
Community Champion
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.
- anupampandey9 years ago
Helper III
vanessafvg your given DAX is giving me the result. This result showing the count of transaction only for last day of month.
Result of DAX provided
I this result if we can show the count of max transaction in that month rather than of lastdate that would be best and desired also.
Regards,
Anupam
- vanessafvg9 years ago
Community Champion
anupampandey the max transaction date is the same as lastdate in this context if you use the month on your graph
please can you rather put in a matrix with the month and the measure as its hard to know what data is contained there
- anupampandey9 years ago
Helper III
Above is the screen shot for your reference. Yellow highlighted part is coming in result and red circled is what is desired.
Thanks,
Anupam
- vanessafvg9 years ago
Community Champion
anupampandey now i get it. sheesh i totally missed that MarcelBeug seems like a good solution
otherwise in dax you will probably need to rank your transaction
see below.
- v-ljerr-msft9 years ago
Microsoft Employee
Hi anupampandey,
Have you tried the solution provided by MarcelBeug above? Does it work in your scenario? If it works, could you accept it as solution to close this thread?
If you still have any question on this issue, feel free to post here. :smileyhappy:
Regards