Power Query: Column and Row Lookup

Pulsar3000

New Member
Joined
Apr 19, 2021
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hello All:

Hope you can all help with my task at hand.
I want to multiply the percentage from the "Criteria" tab that matches the dollar amount for the respective Location, Customer Risk, and Days Old in the "Data" tab.
For example, the corresponding percentage from the "Criteria" tab for cell D3 in the "Data" tab is 2%.
I then want to take this 2% and multiply it by the amount in cell D3 which has $30,000.

I know merge queries in Power Query allows for matching columns and in my case this would work for the Location and Risk column.
However, how do I do the three-way match in Power Query when the the "days old" item is in a row?

PQ Row Lookup.xlsx
ABCDEFG
1LocationCustomerCustomer Risk0 - 30 Days31 - 60 Days61 - 90 Days90+ Days Old
2NorthAdamLow$20,000$15,000$10,000$5,000
3NorthJohnHigh$30,000$25,000$20,000$15,000
4NorthDavidMedium$40,000$35,000$30,000$25,000
5NorthAndrewLow$50,000$45,000$40,000$35,000
6SouthSeanLow$60,000$55,000$50,000$45,000
7SouthJosephHigh$70,000$65,000$60,000$55,000
8SouthIshmaelMedium$80,000$75,000$70,000$65,000
9SouthParkerLow$90,000$85,000$80,000$75,000
Data

PQ Row Lookup.xlsx
ABCDEF
1LocationRisk0 - 30 Days31 - 60 Days61 - 90 Days90+ Days Old
2NorthLow0.50%0.75%1.00%1.25%
3NorthMedium1.00%1.50%2.00%2.50%
4NorthHigh2.00%2.50%3.00%3.50%
5SouthLow0.70%0.95%1.20%1.45%
6SouthMedium1.20%1.70%2.20%2.70%
7SouthHigh2.20%2.70%3.20%3.70%
Criteria
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
try this.

Put the below formula in a cell you want, then copy it to right and downward.

Excel Formula:
=SUMPRODUCT((Criteria!$A$2:$A$7=Data!$A2)*(Criteria!$B$2:$B$7=Data!$C2)*Criteria!C$2:C$7)*Data!D2
 
Last edited:
Upvote 0
or this
Excel Formula:
=SUMIFS(Criteria!C$2:C$7,Criteria!$A$2:$A$7,Data!$A2,Criteria!$B$2:$B$7,Data!$C2)*Data!D2
 
Upvote 0
try this.

Put the below formula in a cell you want, then copy it to right and downward.

Excel Formula:
=SUMPRODUCT((Criteria!$A$2:$A$7=Data!$A2)*(Criteria!$B$2:$B$7=Data!$C2)*Criteria!C$2:C$7)*Data!D2
Not sure if I misunderstood your response, but I'm looking for a Power Query solution.
 
Upvote 0
Let me know how you want to do this.
My Table names are:
• tblCustomers
• tblPerCent

If you use those names you can create 2 blank queries and post in the code below OR load each one into PQ and then replace the code for each with the code below.
(Ideally do query 2 below first)

1) Query - tblCustomersCalc - Close and Load to Worksheet

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblCustomers"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Location", "Customer", "Customer Risk", "Index"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Location", type text}, {"Customer", type text}, {"Customer Risk", type text}, {"Attribute", type text}, {"Value", Currency.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Location", "Customer Risk", "Attribute"}, tblPerCent, {"Location", "Risk", "Attribute"}, "tblPerCent", JoinKind.LeftOuter),
    #"Expanded tblPerCent" = Table.ExpandTableColumn(#"Merged Queries", "tblPerCent", {"Value"}, {"Value.1"}),
    RenamedColumnsPerCent = Table.RenameColumns(#"Expanded tblPerCent",{{"Value.1", "PerCent"}}),
    #"Inserted Multiplication" = Table.AddColumn(RenamedColumnsPerCent, "Multiplication", each [Value] * [PerCent], Currency.Type),
    RoundingNormal = Table.TransformColumns(#"Inserted Multiplication",{{"Multiplication", each Number.Round(_, 2), Currency.Type}}),
    RenamedColumnsCalc = Table.RenameColumns(RoundingNormal,{{"Multiplication", "Calculated Amt"}}),
    RemovedColumnsOtherValues = Table.RemoveColumns(RenamedColumnsCalc,{"Value", "PerCent"}),
    #"Pivoted Column" = Table.Pivot(RemovedColumnsOtherValues, List.Distinct(RemovedColumnsOtherValues[Attribute]), "Attribute", "Calculated Amt", List.Sum),
    SortedRowsOrigOrder = Table.Sort(#"Pivoted Column",{{"Index", Order.Ascending}}),
    RemovedColumnsIndex = Table.RemoveColumns(SortedRowsOrigOrder,{"Index"})
in
    RemovedColumnsIndex

2) Query - tblPerCent - Close and Load Connection Only

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblPerCent"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Location", "Risk"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Percentage.Type}, {"Location", type text}, {"Risk", type text}})
in
    #"Changed Type"

Result

20221215 PQ Multiply Columns Rows Pulsar3000.xlsx
IJKLMNO
1LocationCustomerCustomer Risk0 - 30 Days31 - 60 Days61 - 90 Days90+ Days Old
2NorthAdamLow100112.510062.5
3NorthJohnHigh600625600525
4NorthDavidMedium400525600625
5NorthAndrewLow250337.5400437.5
6SouthSeanLow420522.5600652.5
7SouthJosephHigh1540175519202035
8SouthIshmaelMedium960127515401755
9SouthParkerLow630807.59601087.5
Data
 
Upvote 0
Solution
Let me know how you want to do this.
My Table names are:
• tblCustomers
• tblPerCent

If you use those names you can create 2 blank queries and post in the code below OR load each one into PQ and then replace the code for each with the code below.
(Ideally do query 2 below first)

1) Query - tblCustomersCalc - Close and Load to Worksheet

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblCustomers"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Location", "Customer", "Customer Risk", "Index"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Location", type text}, {"Customer", type text}, {"Customer Risk", type text}, {"Attribute", type text}, {"Value", Currency.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Location", "Customer Risk", "Attribute"}, tblPerCent, {"Location", "Risk", "Attribute"}, "tblPerCent", JoinKind.LeftOuter),
    #"Expanded tblPerCent" = Table.ExpandTableColumn(#"Merged Queries", "tblPerCent", {"Value"}, {"Value.1"}),
    RenamedColumnsPerCent = Table.RenameColumns(#"Expanded tblPerCent",{{"Value.1", "PerCent"}}),
    #"Inserted Multiplication" = Table.AddColumn(RenamedColumnsPerCent, "Multiplication", each [Value] * [PerCent], Currency.Type),
    RoundingNormal = Table.TransformColumns(#"Inserted Multiplication",{{"Multiplication", each Number.Round(_, 2), Currency.Type}}),
    RenamedColumnsCalc = Table.RenameColumns(RoundingNormal,{{"Multiplication", "Calculated Amt"}}),
    RemovedColumnsOtherValues = Table.RemoveColumns(RenamedColumnsCalc,{"Value", "PerCent"}),
    #"Pivoted Column" = Table.Pivot(RemovedColumnsOtherValues, List.Distinct(RemovedColumnsOtherValues[Attribute]), "Attribute", "Calculated Amt", List.Sum),
    SortedRowsOrigOrder = Table.Sort(#"Pivoted Column",{{"Index", Order.Ascending}}),
    RemovedColumnsIndex = Table.RemoveColumns(SortedRowsOrigOrder,{"Index"})
in
    RemovedColumnsIndex

2) Query - tblPerCent - Close and Load Connection Only

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblPerCent"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Location", "Risk"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Percentage.Type}, {"Location", type text}, {"Risk", type text}})
in
    #"Changed Type"

Result

20221215 PQ Multiply Columns Rows Pulsar3000.xlsx
IJKLMNO
1LocationCustomerCustomer Risk0 - 30 Days31 - 60 Days61 - 90 Days90+ Days Old
2NorthAdamLow100112.510062.5
3NorthJohnHigh600625600525
4NorthDavidMedium400525600625
5NorthAndrewLow250337.5400437.5
6SouthSeanLow420522.5600652.5
7SouthJosephHigh1540175519202035
8SouthIshmaelMedium960127515401755
9SouthParkerLow630807.59601087.5
Data

Let me know how you want to do this.
My Table names are:
• tblCustomers
• tblPerCent

If you use those names you can create 2 blank queries and post in the code below OR load each one into PQ and then replace the code for each with the code below.
(Ideally do query 2 below first)

1) Query - tblCustomersCalc - Close and Load to Worksheet

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblCustomers"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Location", "Customer", "Customer Risk", "Index"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Location", type text}, {"Customer", type text}, {"Customer Risk", type text}, {"Attribute", type text}, {"Value", Currency.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Location", "Customer Risk", "Attribute"}, tblPerCent, {"Location", "Risk", "Attribute"}, "tblPerCent", JoinKind.LeftOuter),
    #"Expanded tblPerCent" = Table.ExpandTableColumn(#"Merged Queries", "tblPerCent", {"Value"}, {"Value.1"}),
    RenamedColumnsPerCent = Table.RenameColumns(#"Expanded tblPerCent",{{"Value.1", "PerCent"}}),
    #"Inserted Multiplication" = Table.AddColumn(RenamedColumnsPerCent, "Multiplication", each [Value] * [PerCent], Currency.Type),
    RoundingNormal = Table.TransformColumns(#"Inserted Multiplication",{{"Multiplication", each Number.Round(_, 2), Currency.Type}}),
    RenamedColumnsCalc = Table.RenameColumns(RoundingNormal,{{"Multiplication", "Calculated Amt"}}),
    RemovedColumnsOtherValues = Table.RemoveColumns(RenamedColumnsCalc,{"Value", "PerCent"}),
    #"Pivoted Column" = Table.Pivot(RemovedColumnsOtherValues, List.Distinct(RemovedColumnsOtherValues[Attribute]), "Attribute", "Calculated Amt", List.Sum),
    SortedRowsOrigOrder = Table.Sort(#"Pivoted Column",{{"Index", Order.Ascending}}),
    RemovedColumnsIndex = Table.RemoveColumns(SortedRowsOrigOrder,{"Index"})
in
    RemovedColumnsIndex

2) Query - tblPerCent - Close and Load Connection Only

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblPerCent"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Location", "Risk"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Percentage.Type}, {"Location", type text}, {"Risk", type text}})
in
    #"Changed Type"

Result

20221215 PQ Multiply Columns Rows Pulsar3000.xlsx
IJKLMNO
1LocationCustomerCustomer Risk0 - 30 Days31 - 60 Days61 - 90 Days90+ Days Old
2NorthAdamLow100112.510062.5
3NorthJohnHigh600625600525
4NorthDavidMedium400525600625
5NorthAndrewLow250337.5400437.5
6SouthSeanLow420522.5600652.5
7SouthJosephHigh1540175519202035
8SouthIshmaelMedium960127515401755
9SouthParkerLow630807.59601087.5
Data
Thank You! I went ahead and copied the queries.
I get the below error. Any idea how to fix?

1671086745048.png

Let me know how you want to do this.
My Table names are:
• tblCustomers
• tblPerCent

If you use those names you can create 2 blank queries and post in the code below OR load each one into PQ and then replace the code for each with the code below.
(Ideally do query 2 below first)

1) Query - tblCustomersCalc - Close and Load to Worksheet

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblCustomers"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Added Index", {"Location", "Customer", "Customer Risk", "Index"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Location", type text}, {"Customer", type text}, {"Customer Risk", type text}, {"Attribute", type text}, {"Value", Currency.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Location", "Customer Risk", "Attribute"}, tblPerCent, {"Location", "Risk", "Attribute"}, "tblPerCent", JoinKind.LeftOuter),
    #"Expanded tblPerCent" = Table.ExpandTableColumn(#"Merged Queries", "tblPerCent", {"Value"}, {"Value.1"}),
    RenamedColumnsPerCent = Table.RenameColumns(#"Expanded tblPerCent",{{"Value.1", "PerCent"}}),
    #"Inserted Multiplication" = Table.AddColumn(RenamedColumnsPerCent, "Multiplication", each [Value] * [PerCent], Currency.Type),
    RoundingNormal = Table.TransformColumns(#"Inserted Multiplication",{{"Multiplication", each Number.Round(_, 2), Currency.Type}}),
    RenamedColumnsCalc = Table.RenameColumns(RoundingNormal,{{"Multiplication", "Calculated Amt"}}),
    RemovedColumnsOtherValues = Table.RemoveColumns(RenamedColumnsCalc,{"Value", "PerCent"}),
    #"Pivoted Column" = Table.Pivot(RemovedColumnsOtherValues, List.Distinct(RemovedColumnsOtherValues[Attribute]), "Attribute", "Calculated Amt", List.Sum),
    SortedRowsOrigOrder = Table.Sort(#"Pivoted Column",{{"Index", Order.Ascending}}),
    RemovedColumnsIndex = Table.RemoveColumns(SortedRowsOrigOrder,{"Index"})
in
    RemovedColumnsIndex

2) Query - tblPerCent - Close and Load Connection Only

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="tblPerCent"]}[Content],
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Location", "Risk"}, "Attribute", "Value"),
    #"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Value", Percentage.Type}, {"Location", type text}, {"Risk", type text}})
in
    #"Changed Type"

Result

20221215 PQ Multiply Columns Rows Pulsar3000.xlsx
IJKLMNO
1LocationCustomerCustomer Risk0 - 30 Days31 - 60 Days61 - 90 Days90+ Days Old
2NorthAdamLow100112.510062.5
3NorthJohnHigh600625600525
4NorthDavidMedium400525600625
5NorthAndrewLow250337.5400437.5
6SouthSeanLow420522.5600652.5
7SouthJosephHigh1540175519202035
8SouthIshmaelMedium960127515401755
9SouthParkerLow630807.59601087.5
Data

Thank You! I went ahead and copied the queries.
I get the below error. Any idea on how to fix it?

1671086865868.png
 
Upvote 0
Have you made both data ranges actual Excel Tables ?
Have you given them the same names that I have per my previous post ?
 
Upvote 0
The solution in Power Query is to convert the data in rows into columns by unpivoting and then pivoting back.
So basically we can't do a row lookup in Power Query?
 
Upvote 0

Forum statistics

Threads
1,215,261
Messages
6,123,945
Members
449,134
Latest member
NickWBA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top