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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Power Query Custom Column Lookup Value From Columns In Another Table

I want to create 2x custom columns that will lookup an item id from table 1 and return the read scan date based on conditions of table 2 columns. So the lookup logic for the custom columns should be:

 

  • custom column A - return MIN (earliest) Read Scan Date if Site ID = "NZAKLA" and Reader Desc = "Reader at the office entrance". If item has NO Read Scan Date at the office entrance, then return "No Scan"
  • custom column B - return MAX (last) Read Scan Date if Site ID = "NZAKLA" and Reader Desc = "Reader at the office exit". If item has NO Read Scan Date at the office exit, then return "No Scan"

TABLE 1

Item IDCustom Column A (NZAKLA Entrance Scan)Custom Column B (NZAKLA Exit Scan)
201502869  

TABLE 2

Item IDRead Scan DateSite IDReader Desc
20150286921/04/2020 13:44NZAKLAReader at the office entrance
20150286921/04/2020 13:44NZAKLAReader at the office entrance
20150286921/04/2020 13:48NZAKLAReader at the office entrance
20150286921/04/2020 14:05NZAKLAReader at office entrance and exit
20150286921/04/2020 14:06NZAKLAReader at office entrance and exit
20150286921/04/2020 14:07NZAKLAReader at the customs entrance
20150286921/04/2020 16:25NZAKLAReader at the office exit
20150286921/04/2020 17:38NZAKL2Reader at the office entrance
20150286922/04/2020 0:57NZAKL2Process
20150286923/04/2020 0:31NP0003Reader at the office exit
20150286924/04/2020 22:20NP0003Reader at the office exit

 

8 REPLIES 8
AlB
Super User
Super User

@Anonymous 

#"Added Custom" and #"Added Custom2" are the steps you are looking for. Assumes Table2 as shown in your example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwNDUwsjCzVIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item ID" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item ID", Int64.Type}}),

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom Column A (NZAKLA Entrance Scan)", each let res_=List.Min(Table.SelectRows(Table2, (inner)=> inner[Item ID] = [Item ID] and inner[Site ID]="NZAKLA" and inner[Reader Desc]="Reader at the office entrance")[Read Scan Date]), output_=if res_= null then "No scan" else res_ in output_),
    #"Added Custom2" = Table.AddColumn(#"Added Custom", "Custom Column B (NZAKLA Exit Scan)", each let res_=List.Max(Table.SelectRows(Table2, (inner)=> inner[Item ID] = [Item ID] and inner[Site ID]="NZAKLA" and inner[Reader Desc]="Reader at the office exit")[Read Scan Date]), output_=if res_= null then "No scan" else res_ in output_)

in
    #"Added Custom2"

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

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

Cheers 

SU18_powerbi_badge

 

Anonymous
Not applicable

Hi @AlB, thanks that worked however the calculation / refresh takes a very long time as I assume its due to the size of table 2 with 50k rows....and I intend to add more custom columns with similar formula that you provided which may break the report.

Is there another way to make this more efficient?

@Anonymous 

Not sure. I'd need the full data to play a bit with it. If you don't wan to make it public you can share the link by private message

Have you tried to do this in DAX, creating a calculated table? I believe it would be faster

Please mark the question solved when done and consider giving kudos if posts are helpful.

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

Cheers 

SU18_powerbi_badge

Hi @Anonymous 

I haven't yet had much time to review the M code. Perhaps @ImkeF@edhans have ideas on how to make it faster.

In any case you can try in DAX as well:

1. Build a query in M that returns just the "Item ID" column (like the first column only in the first table you showed) and load it onto the data model, name "Flat File data"

2. Create calculated columns on the "Flat File data" table:

=
VAR res_ = CALCULATE (
    MIN ( 'Raw RFID Data 1'[Read_Dt] );
    FILTER (
        ALL ( 'Raw RFID Data 1'[Item_Id] );
        'Raw RFID Data 1'[Item_Id] = 'Flat File Data'[Item_Id]
    );
    'Raw RFID Data 1'[Reader_Pse] = "Reader at the office entrance";
    'Raw RFID Data 1'[Site_Id] = "NZAKLA"
)
RETURN 
IF(ISBLANK(res_); "No scan";res_)

 

=
VAR res_ = CALCULATE (
    MIN ( 'Raw RFID Data 1'[Read_Dt] );
    FILTER (
        ALL ( 'Raw RFID Data 1'[Item_Id] );
        'Raw RFID Data 1'[Item_Id] = 'Flat File Data'[Item_Id]
    );
    'Raw RFID Data 1'[Reader_Pse] = "Reader at the office exit";
    'Raw RFID Data 1'[Site_Id] = "NZAKLA"
)
RETURN 
IF(ISBLANK(res_); "No scan";res_)

 

Please mark the question solved when done and consider giving kudos if posts are helpful.

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

Cheers 

SU18_powerbi_badge

Anonymous
Not applicable

@AlB thank you it works and much quicker execution/load time, however the RETURN does not work as intended i.e. if Read_Dt blank, then return "No Scan", instead its returning blank. Your thoughts?

Note: I had to wrap function with FORMAT date time as there was an error without it.

 

Annotation 2020-06-03 162716.png

 

 

 

Hi @Anonymous ,

this code should run faster:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwNDUwsjCzVIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item ID" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item ID", Int64.Type}}),
    FilterForCol1 = Table.SelectRows(Table2, each ([Site ID] = "NZAKLA") and ([Reader Desc] = "Reader at the office entrance")),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Item ID"}, FilterForCol1, {"Item ID"}, "FilterForCol1", JoinKind.LeftOuter),
    AddColA = Table.AddColumn(#"Merged Queries", "ColA", each List.Min([FilterForCol1][Read Scan Date])),
    FilterForColB = Table.SelectRows(Table2, each ([Site ID] = "NZAKLA") and ([Reader Desc] = "Reader at the office exit")),
    #"Merged Queries1" = Table.NestedJoin(AddColA, {"Item ID"}, FilterForColB, {"Item ID"}, "FilterForColB", JoinKind.LeftOuter),
    AddColB = Table.AddColumn(#"Merged Queries1", "ColB", each List.Max([FilterForColB][Read Scan Date])),
    #"Replaced Value" = Table.ReplaceValue(AddColB,null,"No Scan",Replacer.ReplaceValue,{"ColA", "ColB"}),
    #"Removed Columns" = Table.RemoveColumns(#"Replaced Value",{"FilterForCol1", "FilterForColB"})
in
    #"Removed Columns"

Problem with the other code is that the whole Table2 will be adressed with every row of your Table1.

I'm using 2 merges here, so the Table2 will just be reference twice.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

AlB
Super User
Super User

Hi @Anonymous 

Can you provide an example based on your dat showing the expected result?  For instance, what would the expected result be for ID 201502869 and the rationale?

Please mark the question solved when done and consider giving kudos if posts are helpful.

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

Cheers 

SU18_powerbi_badge

Anonymous
Not applicable

Hi @AlB, so for the sample data provided, the expected results for item ID 201502869 should be:

  • for column A (office entrance scan) is 21/04/2020 13:44
  • for column B (office exit scan) is 21/04/2020 16:25

Hope that makes sense...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors