Forum Discussion
power query/ dax measure , multiple conditions/ multiple answers
I have loaded multiple tables in power query
table of revenue per category:
| category | revenue |
| EM01 | 86.000 |
| EM02 | 68.000 |
| EM03 | 93.000 |
| EM04 | 87.000 |
| EM05 | 6.000 |
| EM06 | 96.000 |
| EM07 | 87.000 |
| EM08 | 54.000 |
table of share per category per accountmanager:
| employee | accountmanager | share accountmanager |
| EM01 | AM1 | 75% |
| EM01 | AM2 | 25% |
| EM02 | AM1 | 100% |
| EM03 | AM2 | 100% |
| EM04 | AM3 | 100% |
| EM05 | AM2 | 60% |
| EM05 | AM3 | 40% |
| EM06 | AM1 | 100% |
| EM07 | AM1 | 50% |
| EM07 | AM3 | 50% |
| EM08 | AM2 | 100% |
Note: in same cases (EM01, EM05, EM07) multiple accountmanagers are in charge for a category. Total of share per category is always 100%.
How to organize in power query to connect both tables to get table of revenue per per category per accountmanager? Use measure? Which? Or other solution?
Answer (in pivot table) should be:
| employee | AM1 | AM2 | AM3 | total |
| EM01 | 64.500 | 21.500 | - | 86.000 |
| EM02 | 68.000 | - | - | 68.000 |
| EM03 | - | 93.000 | - | 93.000 |
| EM04 | - | - | 87.000 | 87.000 |
| EM05 | - | 3.600 | 2.400 | 6.000 |
| EM06 | 96.000 | - | - | 96.000 |
| EM07 | 43.500 | - | 43.500 | 87.000 |
| EM08 | - | 54.000 | - | 54.000 |
You may use measure like
Shared := VAR TotalRevenue = SUMMARIZE ( Revenue, Revenue[category], "Revenue", SUM ( Revenue[revenue] ) ) VAR AddShares = ADDCOLUMNS ( CROSSJOIN ( TotalRevenue, Shares ), "ToTake", Revenue[category] = Shares[employee] ) VAR Filtered = FILTER ( AddShares, [ToTake] ) VAR KeepColumns = SELECTCOLUMNS ( Filtered, "Employee", [employee], "Manager", [accountmanager], "Revenue", [revenue], "Share, %", [share accountmanager] ) VAR Result = ADDCOLUMNS ( KeepColumns, "Shared", [Revenue] * [Share, %] ) RETURN SUMX ( Result, [Shared] )For Power Query only no changes are required
16 Replies
- IlirUBrass Contributor
I'm not sure if you want a solution through formulas, but if so, then apply below formula.
=PIVOTBY(D2:D12, E2:E12, XLOOKUP(D2:D12, A2:A9, B2:B9) * F2:F12, SUM,, 0,, 1)
Hope this helps.
- SergeiBaklanDiamond Contributor
It shall be modified a bit since category values are not unique. As variant, like
=LET( GroupRevenue, GROUPBY(Revenue[category],Revenue[revenue], SUM,,0), Shared, XLOOKUP(Shares[employee], CHOOSECOLS(GroupRevenue,1), CHOOSECOLS(GroupRevenue,2)) * Shares[share accountmanager], PIVOTBY(Shares[employee], Shares[accountmanager], Shared, SUM) )- IlirUBrass Contributor
How about this?
=LET(pb, PIVOTBY(D2:D12, E2:E12, XLOOKUP(D2:D12, A2:A9, B2:B9) * F2:F12, SUM,, 0,, 1), VSTACK(pb, IFERROR(1 / (1 / BYCOL(DROP(pb, 1), SUM)), "Total")))or more shorter is this:
=PIVOTBY(D2:D12, E2:E12, XLOOKUP(D2:D12, A2:A9, B2:B9) * F2:F12, SUM,, 1,, 1)I think both works to give same results as your formula.
(see my screenshot given in preview post).
BR
IlirU
- SergeiBaklanDiamond Contributor
Power Query variant with Grand Total
let Source = Table.Join(Shares, {"employee"}, Revenue, {"category"}, JoinKind.LeftOuter), AddShared = Table.AddColumn(Source, "Shared", each [share accountmanager]*[revenue], Currency.Type), SelectColumns = Table.SelectColumns(AddShared,{"employee", "accountmanager", "Shared"}), PivotManagers = Table.Pivot( SelectColumns, List.Distinct(SelectColumns[accountmanager]), "accountmanager", "Shared", List.Sum), AddTotal = Table.AddColumn( PivotManagers, "Total", each List.Sum ( List.Skip( Record.FieldValues(_) ) ), Currency.Type), GrandTotal = Table.FromColumns( {{"Grand Total"}} & List.Transform( List.Skip( Table.ToColumns( AddTotal ) ), (q) => {List.Sum(q)} ), Table.ColumnNames(AddTotal) ), Result = AddTotal & GrandTotal, DeclareType = Table.TransformColumnTypes( Result, List.Zip({ {Table.ColumnNames( Result ){0}}, {type text} } ) & List.Transform( List.Skip( Table.ColumnNames( Result ) ), (q) => { q, Currency.Type } ) ) in DeclareType- Niels_83Copper Contributor
And what if there are multiple rows per EM in revenue table? Then it's not possible to make a connections between revenue table and AM table.
- SergeiBaklanDiamond Contributor
You may use measure like
Shared := VAR TotalRevenue = SUMMARIZE ( Revenue, Revenue[category], "Revenue", SUM ( Revenue[revenue] ) ) VAR AddShares = ADDCOLUMNS ( CROSSJOIN ( TotalRevenue, Shares ), "ToTake", Revenue[category] = Shares[employee] ) VAR Filtered = FILTER ( AddShares, [ToTake] ) VAR KeepColumns = SELECTCOLUMNS ( Filtered, "Employee", [employee], "Manager", [accountmanager], "Revenue", [revenue], "Share, %", [share accountmanager] ) VAR Result = ADDCOLUMNS ( KeepColumns, "Shared", [Revenue] * [Share, %] ) RETURN SUMX ( Result, [Shared] )For Power Query only no changes are required
- OliverScheurichGold Contributor
An alternative could be Power Query. In the attached file you can add data to the blue dynamic tables. Then you can click in any cell of the green table and right-click with the mouse and select refresh to update the green result table.
- SergeiBaklanDiamond Contributor
As variant you may add both tables to data model with relationship as
Add measure
Shared:=SUMX ( Shares, Shares[share accountmanager] * RELATED ( Revenue[revenue] ) )and build PivotTable from data model using above measure
- Harun24HRBronze Contributor
Here is a formula approach if table are loaded to sheet.
=PIVOTBY(D2:D12,E2:E12,XLOOKUP(D2:D12,A2:A9,B2:B9)*F2:F12,SUM,0,0)