Forum Discussion
Dynamic Table.group
- 6 years ago
I'll try to break it down:
List.Transform(List.Distinct(PreviousStep[CompteNum]), (t) => {Text.From(t), each List.Sum(Table.SelectRows(_, each [CompteNum] = t)[Montant]), type number})to
List.Transform( List.Distinct(PreviousStep[CompteNum]), (t) => { Text.From(t), each List.Sum( Table.SelectRows( _, each [CompteNum] = t ) [Montant] ), type number } )List.Transform takes two arguments: 1) a list and 2) a transformation function. Then, for each of its elements, it applies the transformation function.
List.Distinct ( PreviousStep[CompteNum] ) gives a list of distinct elements in [CompteNum].
(t) => ... is basically the transformation function defined. It takes one argument and returns a list in our case.
I'll give an example:List.Transform( List.Transform({"a", "b"}, (t) => { Text.From(t), each List.Sum( Table.SelectRows( _, each [CompteNum] = t ) [Montant] ), type number } )will return:
{ { "a", each List.Sum( Table.SelectRows( _, each [CompteNum] = "a" ) [Montant] ), type number }, { "b", each List.Sum( Table.SelectRows( _, each [CompteNum] = "b" ) [Montant] ), type number } }
Which is in turn passed as the third argument in Table.Group. I hope it already looks to you like Table.Group's third argument, which, when executed, every _ will be traslated as the grouped table based on the grouping criteria.Best,
Spyros
Hi PhilippeMuniesa ,
Assuming I understood what you'd like to do, you could skip pivoting:
#"Group by PieceRef" = Table.Group(PreviousStep, {"PieceRef"}, List.Transform(List.Distinct(PreviousStep[CompteNum]), (t) => {Text.From(t), each List.Sum(Table.SelectRows(_, each [CompteNum] = t)[Montant]), type number}))
Where PreviousStep is your last query step.
Beware that you're going to lose all other columns. If you want to keep them, you should add them in the grouping columns; the {"PieceRef"} part.
Cheers
edhansI had the same issue, but pasting the table first in excel and copying it again to paste it in PBI's "Enter Data" is the quickest way to fix that. Excel is smarter when pasting something that looks like a table.
Here is the table in case you'd like to try something:
Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZJBasMwEEWvIryO8YwkW9ZSVRSiYuLiKIYSsijGkECoIfGm616mvUZ7saoudpOu4npWM/Clh95ouw1KE8wCjghdoR905ojOs8xoZxd+LtX9o/LlW5ARYEQB5XeuMHppiHLOus3cEHQoSUgWGoDRxAcYCiHjYDfrKTwWPxw/uFKRqjke66r9fP14r6eAwpjKJE5/SQIS4D0pa17qE6lOHvNW7aeBaMJTKunFm24wd+dbChecwqyd2hTK2Xw1HCh7ng8g5ZiK0ebGg0IKiRR/zLFrc4U1kxAIjKdjnenu/0TAes5aW6JWzj7kmRl2IqCTRSkf7WoEwEti4lrRANBNezg/tYfm+UyW7n/XI3TL3n0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [JournalCode = _t, CompteNum = _t, CompteLib = _t, PieceRef = _t, PieceDate = _t, EcritureLib = _t, Montant = _t])
Smauro thanks for the data. I did try Excel and it was the same single column of data. Dunno why..
That said, PhilippeMuniesa can you explain what your expected result is? Like Smauro said, you shouldn't unpivot. Just grab the Compteum column and group, or grab a few columns. For example,
- Original data
-
- Group by CompteNum and CompteLib, summarize Montant
-
If that doesn't help, can you please post what you expect the results to be? In general, the pivot and unpivot functions are use to normalize data, not denormalize it, and your data looks normalized to me, which is good.
The M code for the grouping I did is as follows:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZJBasMwEEWvIryO8YwkW9ZSVRSiYuLiKIYSsijGkECoIfGm616mvUZ7saoudpOu4npWM/Clh95ouw1KE8wCjghdoR905ojOs8xoZxd+LtX9o/LlW5ARYEQB5XeuMHppiHLOus3cEHQoSUgWGoDRxAcYCiHjYDfrKTwWPxw/uFKRqjke66r9fP14r6eAwpjKJE5/SQIS4D0pa17qE6lOHvNW7aeBaMJTKunFm24wd+dbChecwqyd2hTK2Xw1HCh7ng8g5ZiK0ebGg0IKiRR/zLFrc4U1kxAIjKdjnenu/0TAes5aW6JWzj7kmRl2IqCTRSkf7WoEwEti4lrRANBNezg/tYfm+UyW7n/XI3TL3n0B", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [JournalCode = _t, CompteNum = _t, CompteLib = _t, PieceRef = _t, PieceDate = _t, EcritureLib = _t, Montant = _t]),
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"PieceDate", type date}}, "en-BS"),
#"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"Montant", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"CompteNum", "CompteLib"}, {{"Total", each List.Sum([Montant]), type number}})
in
#"Grouped Rows"
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.