cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
Anonymous
Not applicable

How to merge two get nearest previous date by using M Code ?

Hi. I am trying to get nearest previous date from other table(Table1) in Power Query not by DAX?

 

Table1:

proscons_0-1660396385126.png

 

Expected Result :

Table2 (added Column1 form Table2):

proscons_2-1660396517362.png

 

 

Any help on how to do this would be highly appreciated.

Thank you.

 

2 ACCEPTED SOLUTIONS
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

This code is for Table2.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQ1NFWK1YFxgQiViyJrDBRB5RoqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    BuffList = List.Buffer(Table1[Column1]),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [l=List.Transform(BuffList,(x)=>Duration.Days(x-[Column1])),p=BuffList{List.PositionOf(l,List.Last(List.Select(l,(y)=>y<=0)))}][p])
in
    #"Added Custom"

 Code for Table1

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

View solution in original post

You have done in the right manner. As supplying both input and output makes us give right answer.

You will need to use a try otherwise block

try [l=List.Transform(BuffList,(x)=>Duration.Days(x-[Column1])),p=BuffList{List.PositionOf(l,List.Last(List.Select(l,(y)=>y<=0)))}][p] otherwise List.Max(BuffList)

Hence, following code for Table2

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQ1NFWK1YFxgQiViyJrDBRB5RrCuEBzjMDcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    BuffList = List.Buffer(Table1[Column1]),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each try [l=List.Transform(BuffList,(x)=>Duration.Days(x-[Column1])),p=BuffList{List.PositionOf(l,List.Last(List.Select(l,(y)=>y<=0)))}][p] otherwise List.Max(BuffList))
in
    #"Added Custom"

View solution in original post

9 REPLIES 9
Vijay_A_Verma
Super User
Super User

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

This code is for Table2.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQ1NFWK1YFxgQiViyJrDBRB5RoqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    BuffList = List.Buffer(Table1[Column1]),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each [l=List.Transform(BuffList,(x)=>Duration.Days(x-[Column1])),p=BuffList{List.PositionOf(l,List.Last(List.Select(l,(y)=>y<=0)))}][p])
in
    #"Added Custom"

 Code for Table1

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MAQipVgdGNdI19AAiWsMFFGKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}})
in
    #"Changed Type"
Anonymous
Not applicable

@Vijay_A_Verma 
I am sorry for asking again.

I got the error for date before Table1.

Could you please advice me how to change formula?

 

I would like to get last date(2022/03/02) in Table1 when date is prior to 2022/01/01.  

Table1:

proscons_2-1660455101676.png

 

Expected Result :

Table2 (added Column1 form Table2):

proscons_1-1660455045579.png

 

I know how to replace the error value, but I would like to know how to do it in this formula if possible😣


Solved: Query Editor-Replace Errors in Date column with la... - Microsoft Power BI Community

The way you showed input and output in your original problem statement, in the same way you need to post your requirement.

Anonymous
Not applicable

@Vijay_A_Verma 
I have edited my post.

Sorry if my understanding is different.

I am not a native speakes so I'm not sure if I understand it properly..😓

You have done in the right manner. As supplying both input and output makes us give right answer.

You will need to use a try otherwise block

try [l=List.Transform(BuffList,(x)=>Duration.Days(x-[Column1])),p=BuffList{List.PositionOf(l,List.Last(List.Select(l,(y)=>y<=0)))}][p] otherwise List.Max(BuffList)

Hence, following code for Table2

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQ1NFWK1YFxgQiViyJrDBRB5RrCuEBzjMDcWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    BuffList = List.Buffer(Table1[Column1]),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each try [l=List.Transform(BuffList,(x)=>Duration.Days(x-[Column1])),p=BuffList{List.PositionOf(l,List.Last(List.Select(l,(y)=>y<=0)))}][p] otherwise List.Max(BuffList))
in
    #"Added Custom"
Anonymous
Not applicable

Thank you so much!!

It worked perfectly😳

If you make sure BuffList is sorted, then you can simply take the last element that's less than or equal to the date in the current row.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtI1MNQ1NFWK1YFxgQiViyJrDBRB5RoqxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), type table [Column1 = text]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type date}}),
    BuffList = List.Buffer(List.Sort(Table1[Column1])),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom",
        (row) => List.Last(List.Select(BuffList, each _ <= row[Column1])), type date)
in
    #"Added Custom"

 

 

Anonymous
Not applicable

@AlexisOlson 

Thank you!I’ll try that too!

Anonymous
Not applicable

@Vijay_A_Verma 
Thank you for helping me.

I didn't know this solution.
<List.Buffer(Table1[Column1])>

Thank you 😊

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors