Forum Discussion
Csalinas144
Helper II
5 years agoAdding a Custom Column with condition.
I have two data sets (A and B). I want to Add a Custom Column with condition to Data Set B. Custom Column added to Data Set B. If input in Data Set A is after or equal to input in Data Se...
- 5 years ago
This is the code Csalinas144
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ddJLDoIwEAbgu7AmKTNtAQ+gK9m6IawMMRiFjS68vVNo6c8rodASvs6D1nVCSZqc++fwk2clg6y7ydDKKs54mubTtEnrhFeA2YNCEQchcx2JBnKRYUIMypRc3rgFR2Tk1e37ug9vrygEMoDMwrh9q67vrsNj8GpOz8Z6rCqjyV1yr/bd9p9QkZ4DMQRiiqjYIuNRrmgOJHPoXLlAYyNcJJ5qZwuNYGCnnaKCO86QsqMfOzZsjiULaCARdn2sy+2j/Zd0xHiZ4wYyQjgZpDfxCJhGZoCZ9SEkKM6ggkaS3ckSoT2CyzNyWbscXeFd8wc=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Purchase Order Number" = _t, #"Product Name" = _t, Gender = _t, Age = _t, Quantity = _t, Date = _t, #"Ship Date" = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Ship Date", type date}}), #"Merged Queries" = Table.NestedJoin( #"Changed Type", {"Product Name"}, Prices, {"Product Name"}, "Prices", JoinKind.LeftOuter ), GetPrice = Table.AddColumn( #"Merged Queries", "Price", each let varDate = [Date] in try Table.Max( Table.SelectRows( [Prices], each [Date] < varDate ), "Date" )[#"Price per Unit(US$)"] otherwise 0 ), #"Removed Columns" = Table.RemoveColumns(GetPrice,{"Prices"}) in #"Removed Columns"Note that the first record returned 0 becuase there was no price set for March 5, 2021. Prices didn't start until March 10.
Here is the PBIX though. It will be much clearer what I did since there are two tables involved.
Anonymous
5 years agoNot applicable
OK, then you could still group Table B by product, using the List.Max aggregation, also add an All Rows Aggregation. Name the step Grouped. Name those columns Max Date, and Details.
Now you can add a column:
= Table.AddColumn(Grouped, "TheRecords", each if [Date] >= [Max Date] then Table.Max(Table.FindText(TableA, each [Product]), {"Date"}) else Table.Max(Table.SkipLastN(Table.FindText(TableB, each [Product])), {"Date"}))
--Nate