Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Oleksandr
New Member

Filtering one table inside the scope of AddColumn

Hi experts!
Can someone point me in the right direction: I have two tables: the first is the main one and the second is supplementary, which contains different coefficients applicable based on the value of the column from the main table. Then the coefficient serves as a multiplier to the product of two values from the main table.

 

Main table

Col1Col2Col3Added Column
12ACol1*Col2*Lookup in Table 2 based on Col3
34BCol1*Col2*Lookup in Table 2 based on Col3


Reference Table

ValueCoefficient
A1
B0.5

 

So far I come up with the following solution, which does not work because the reference to the value inside the Main table does not work inside the scope of the Table.Filter of the reference table.


#"Added Custom" = Table.AddColumn(

        MainTable,

        "Added Column",

        each

            [Col1]*

            [Col2]*

            List.First(

                Table.Column(

                    Table.SelectRows(

                        ReferenceTable,

                        each [Value] = [Col3]                   
                      ),

                    "Coefficient"

                )

            )

    )
Any advice is greatly appreciated!
Best,
Sasha

1 ACCEPTED SOLUTION
edhans
Super User
Super User

You have to assign the value of Col3 to a variable so the inner Table.SelectRows can access it @Oleksandr . See this code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYkcgds7PMdQCEkZaPvn52aUFCpl5CiGJSTmpCkYKSYnFqSkK+XkKQHljpVidaCVjoA4TIHYiTWcsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, #"Added Column" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", Int64.Type}, {"Col2", Int64.Type}}),
    AddFromRefTable = 
        Table.AddColumn(
            #"Changed Type",
            "New Calculation",
            each
                let
                    varCol3 = [Col3]
                in
                Table.SelectRows(
                    #"Reference Table",
                    each [Value] = varCol3
                )[Coefficient]{0} * [Col1] * [Col2]
        )
in
    AddFromRefTable

 

It returns this:

edhans_0-1613938637854.png

Note that this will not perform well over tens of thousands of records as it will have to do tens of thousands of Table.SelectRows, but for smaller data sets it will work fine. This is what it does:

                Table.SelectRows(
                    #"Reference Table",
                    each [Value] = varCol3
                )[Coefficient]{0} * [Col1] * [Col2]

 

  1. Returns a table where [Value] from the Reference table matches varCol3 which was previously assigned the value from [Col3] of the current table.
  2. The [Coefficient] after the close paren of Table.SelectRows returns the Coefficient column as a list.
  3. The {0} returns the first item in that list as a scalar value
  4. That is then multiplied by Col1 and Col2 of the current table.

 

 

For large sets, just merge the Reference table into the main table, expand the Coefficient column, then do your math and remove unnecessary columns. That will do millions of records just fine.

edhans_1-1613938815896.png

Basic code for that just in case you've not done a Merge before. Assumes your 2nd table is called "Reference Table"

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYkcgds7PMdQCEkZaPvn52aUFCpl5CiGJSTmpCkYKSYnFqSkK+XkKQHljpVidaCVjoA4TIHYiTWcsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, #"Added Column" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", Int64.Type}, {"Col2", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Col3"}, #"Reference Table", {"Value"}, "Reference Table", JoinKind.LeftOuter),
    #"Expanded Reference Table" = Table.ExpandTableColumn(#"Merged Queries", "Reference Table", {"Coefficient"}, {"Coefficient"})
in
    #"Expanded Reference Table"

 

 

 

How to use M code provided in a blank query:
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.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

5 REPLIES 5
edhans
Super User
Super User

You have to assign the value of Col3 to a variable so the inner Table.SelectRows can access it @Oleksandr . See this code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYkcgds7PMdQCEkZaPvn52aUFCpl5CiGJSTmpCkYKSYnFqSkK+XkKQHljpVidaCVjoA4TIHYiTWcsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, #"Added Column" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", Int64.Type}, {"Col2", Int64.Type}}),
    AddFromRefTable = 
        Table.AddColumn(
            #"Changed Type",
            "New Calculation",
            each
                let
                    varCol3 = [Col3]
                in
                Table.SelectRows(
                    #"Reference Table",
                    each [Value] = varCol3
                )[Coefficient]{0} * [Col1] * [Col2]
        )
in
    AddFromRefTable

 

It returns this:

edhans_0-1613938637854.png

Note that this will not perform well over tens of thousands of records as it will have to do tens of thousands of Table.SelectRows, but for smaller data sets it will work fine. This is what it does:

                Table.SelectRows(
                    #"Reference Table",
                    each [Value] = varCol3
                )[Coefficient]{0} * [Col1] * [Col2]

 

  1. Returns a table where [Value] from the Reference table matches varCol3 which was previously assigned the value from [Col3] of the current table.
  2. The [Coefficient] after the close paren of Table.SelectRows returns the Coefficient column as a list.
  3. The {0} returns the first item in that list as a scalar value
  4. That is then multiplied by Col1 and Col2 of the current table.

 

 

For large sets, just merge the Reference table into the main table, expand the Coefficient column, then do your math and remove unnecessary columns. That will do millions of records just fine.

edhans_1-1613938815896.png

Basic code for that just in case you've not done a Merge before. Assumes your 2nd table is called "Reference Table"

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTICYkcgds7PMdQCEkZaPvn52aUFCpl5CiGJSTmpCkYKSYnFqSkK+XkKQHljpVidaCVjoA4TIHYiTWcsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, #"Added Column" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", Int64.Type}, {"Col2", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Col3"}, #"Reference Table", {"Value"}, "Reference Table", JoinKind.LeftOuter),
    #"Expanded Reference Table" = Table.ExpandTableColumn(#"Merged Queries", "Reference Table", {"Coefficient"}, {"Coefficient"})
in
    #"Expanded Reference Table"

 

 

 

How to use M code provided in a blank query:
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.

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thank you very much! Extencivity of your kind reply fills a lot of gaps in my basic understanding of M! 🙂

Excellent @Oleksandr - I had this exact same question 2 years ago when I was getting started. Glad you are enjoying M. It can be fun to use.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
AlB
Super User
Super User

Hi @Oleksandr 

See the attached file for a possible solution

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

Thank you so much for your help! However, I'm working with Power Query inside of Excel, have not requested a Power Bi desktop yet. So if you could share the code, probably I could figure out from it. Thaks again!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

Top Solution Authors
Top Kudoed Authors