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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
sid-poly
Helper I
Helper I

Compare two columns and bring value of third column

There is a table like 

 

Key ChildKey Parent Date of Completion
1 22/03/2023
2122/04/2023
3414/11/2022
5 19/11/2022
4518/10/2022

 

based on the table above there is a parent key and a child key. Need a column

 

If key child is not null and key parent is null then Date of Completion and if Key child is not null and key parent is not null then give the date based on key parent column. Please find below the required table

 

Key ChildKey Parent Date of CompletionKey Parent DOC
1 22/03/202322/03/2023
2122/04/202322/03/2023
3514/11/202219/11/2022
5 19/11/202219/11/2022
4518/10/202219/11/2022

 

Please help with the power query logic for the above mentioned scenario

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

in DAX, it seems easier

Parent DOC = VAR _p=VALUE(PATHITEM(PATH(SampleData[Key Child],SampleData[Key Parent]),1)) RETURN MAXX(FILTER(SampleData,SampleData[Key Child]=_p),'SampleData'[Date of Completion])
in M, you can create a custom recursion function.
wdx223_Daniel_0-1679970460069.png

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VcxRCsAwCAPQu/hd0EQH3Vmk97/GdCuMQr5eQjIFMqRCqrnS6LJGCouwOX72ougqFGjmy9f3gfvQHnaDqbDN6wE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Key Child" = _t, #"Key Parent" = _t, #"Date of Completion" = _t]),
Custom1 = let fx=(k)=>let a=Source{[Key Child=k]}? ??[] in if a[Key Parent]?=null or a[Key Parent]?="" then a[Date of Completion]? else @Fx(a[Key Parent]) in Table.AddColumn(Source,"Parent DOC",each fx([Key Child]))
in
Custom1

View solution in original post

3 REPLIES 3
wdx223_Daniel
Super User
Super User

in DAX, it seems easier

Parent DOC = VAR _p=VALUE(PATHITEM(PATH(SampleData[Key Child],SampleData[Key Parent]),1)) RETURN MAXX(FILTER(SampleData,SampleData[Key Child]=_p),'SampleData'[Date of Completion])
in M, you can create a custom recursion function.
wdx223_Daniel_0-1679970460069.png

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VcxRCsAwCAPQu/hd0EQH3Vmk97/GdCuMQr5eQjIFMqRCqrnS6LJGCouwOX72ougqFGjmy9f3gfvQHnaDqbDN6wE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Key Child" = _t, #"Key Parent" = _t, #"Date of Completion" = _t]),
Custom1 = let fx=(k)=>let a=Source{[Key Child=k]}? ??[] in if a[Key Parent]?=null or a[Key Parent]?="" then a[Date of Completion]? else @Fx(a[Key Parent]) in Table.AddColumn(Source,"Parent DOC",each fx([Key Child]))
in
Custom1

ronrsnfld
Super User
Super User

The code below gives the results you show (almost) with the following caveats:

  • Line 3 of your results table has `5` for Key Parent, but your data table shows `4`
    • I assumed there should be a `4` in the results table also
  • None of your Key Child cells are null
  • If both Parent and Child are non-null, then return DOC of the same child column that equals the parent
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VczBCcBQCAPQVYrnD5pooZ1F/v5rVItQesjlJSRTIEuOCqnmSqPLXikswnB87EXRVSjQzJfPOcH94152hUthw/sB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Key Child" = _t, #"Key Parent " = _t, #"Date of Completion" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Key Child", Int64.Type}, 
        {"Key Parent ", Int64.Type}, 
        {"Date of Completion", type date}},
        "en-GB"),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Key Parent DOC", each 
        if [#"Key Parent "] = null
          then [Date of Completion]
          else  #"Changed Type"[Date of Completion]
            {List.PositionOf(#"Changed Type"[#"Key Child"],[#"Key Parent "])}, type date)
in
    #"Added Custom"

Data

ronrsnfld_0-1679966130465.png

Results

ronrsnfld_1-1679966308683.png

 

 

 

 

ND_Pard
Helper II
Helper II

I used four (4) Power Queries.  Below are the names I gave each query and the code used to arrive at the solution.

Hi-Light only the 1st three (3) columns of data: Key Child, Key Parent, Date of Completion

Click on the ribbon: Data, under the group: Get and Transform Data, click the: From Table/Range


Once the Power Query Editor opens, on the right side of the screen enter the query name.
Click on the Advanced Editor and replace with the code provided.

Query Name: Data
Note: in the code below, Power Query named my hi-lighted data: "Table" (Yours may name it something else, change the name accordingly.)
Code:
let
Source = Excel.CurrentWorkbook(){[Name="Table"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Key Child", type text}, {"Key Parent", type text}, {"Date of Completion", type date}})
in
#"Changed Type"

Click on "Done"
On the ribbon: Home, click: Close and Load

 

Hi-Light the newly created table and similar to the above, create the next Power Query:
Query Name: Key_Child
Code:
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
#"Remove Key Parent Colmn" = Table.RemoveColumns(Source,{"Key Parent"}),
#"Changed Type" = Table.TransformColumnTypes(#"Remove Key Parent Colmn",{{"Key Child", type text}, {"Date of Completion", type date}})
in
#"Changed Type"

 

Click on "Done"
On the ribbon: Home, click: Close and Load

 

Hi-Light the newly created table and similar to the above, create the next Power Query:
Query Name: Key Parent DOC
Code:
let
Source = Table.NestedJoin(Data, {"Key Parent"}, Key_Child, {"Key Child"}, "Key_Child", JoinKind.LeftOuter),
#"Expanded Key_Child" = Table.ExpandTableColumn(Source, "Key_Child", {"Date of Completion"}, {"Key_Child.Date of Completion"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Key_Child",{"Key Child", "Date of Completion"}),
#"Filtered Out Null Key Parent" = Table.SelectRows(#"Removed Columns", each ([Key Parent] <> null)),
#"Rename Key Parent DOC Colmn" = Table.RenameColumns(#"Filtered Out Null Key Parent",{{"Key_Child.Date of Completion", "Key Parent DOC"}}),
#"Removed Duplicate Key Parents" = Table.Distinct(#"Rename Key Parent DOC Colmn", {"Key Parent"})
in
#"Removed Duplicate Key Parents"

Click on "Done"
On the ribbon: Home, click: Close and Load

 

Hi-Light the newly created table and similar to the above, create the next Power Query:
Query Name: Solution

Code:
let
Source = Table.NestedJoin(Data, {"Key Parent"}, #"Key Parent DOC", {"Key Parent"}, "Key Parent DOC", JoinKind.LeftOuter),
#"Expanded Key Parent DOC" = Table.ExpandTableColumn(Source, "Key Parent DOC", {"Key Parent DOC"}, {"Key Parent DOC.1"}),
#"Added Conditional Column" = Table.AddColumn(#"Expanded Key Parent DOC", "Key Parent DOC", each if [Key Parent DOC.1] = null then [Date of Completion] else [Key Parent DOC.1]),
#"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Key Parent DOC.1"}),
#"Changed Type" = Table.TransformColumnTypes(#"Removed Columns",{{"Key Parent DOC", type date}})
in
#"Changed Type"

Click on "Done"
On the ribbon: Home, click: Close and Load

This final query should be what you're looking for ...

Good Luck

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.