Forum Discussion
Adding a Custom Column with condition.
- 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.
Csalinas144 Maybe:
Column in B =
VAR __Product = [Product Name]
VAR __Date = [Date]
VAR __TableA = FILTER('TableA',[Product Name]=__Product && [Date]>=__Date)
RETURN
IF(
ISBLANK(__TableA),
MINX(FILTER('TableA',[Product Name]=__Product),[Date]),
MAXX(__TableA,[Date])
)Where do I need to fill in my inputs using your statement?
- edhans5 years ago
Community Champion
Did you see the URL in my post above on how to post a table of data? Or you could provide a PBIX file with embedded content and share via OneDrive or Dropbox. I'll repost it, but everything we've ever needed to solve a solution is provided in the below info.
How to get good help fast. Help us help you.
How To Ask A Technical Question If you Really Want An Answer
How to Get Your Question Answered Quickly - Give us a good and concise explanation
How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables. - Csalinas1445 years ago
Helper II
In line 4
VAR __TableA = FILTER('TableA',[Product Name]=__Product && [Date]>=__Date)
What does __Product&&[Date]>=__Date) mean exaclty. Trying to decipher this code.
Can you write between each line what each line is saying in english?
Thank You,
Christian
- Csalinas1445 years ago
Helper II
Greg_Deckler Is it even possible to refer to a different table within a Calculated Column entry for a different table? Seems to not be an option to refer to a column in my other data set when writing this code.
Thank You for your help again!
- edhans5 years ago
Community Champion
It depends Csalinas144 - what do you mean by "calculated column?" You mean a true Calculated Column in DAX? Then yes, use RELATED() or RELATEDTABLE() to access other columns through relationships. If you mean a Custom Column in Power Query, then again, yes, and about 10 different ways to do so. Merging is the easiest, but without data, and using specific terms, hard to know.
How to get good help fast. Help us help you.
How To Ask A Technical Question If you Really Want An Answer
How to Get Your Question Answered Quickly - Give us a good and concise explanation
How to provide sample data in the Power BI Forum - Provide data in a table format per the link, or share an Excel/CSV file via OneDrive, Dropbox, etc.. Provide expected output using a screenshot of Excel or other image. Do not provide a screenshot of the source data. I cannot paste an image into Power BI tables.