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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Xilalus
Frequent Visitor

Replace with latest not zero value (Power BI)

Hey guys,
I have a table from a data source, which contains exchange rates from EUR to whatevery other currency.

Obviously this table only contains exchange rates for months that have already finished.

But I want to use the latest exchange rate for a currency for future months as well, as long as I don't get one from the data source.

 

The data source already delivers many rows with 0 as exchange rate, so my idea would be to somehow copy the latest value for each currency into those rows.

 

Here an example how the table is structured:

Xilalus_0-1664372910131.png

 

I use the first column to join the exchange rates table with other tables if they need some currencies exchanged.

 

Thank you for your time 🙂

1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @Xilalus ,

 

Paste this over the defult code in Advanced Editor to follow my steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnZ00TXXNTIwMlLSUTI30zM2UYrVgQhbIAkbmcOFLWHCBnAhQwMsYoZIYrEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [currencyMonth = _t, exchRate = _t]),
    chgInitialTypes = Table.TransformColumnTypes(Source,{{"currencyMonth", type text}, {"exchRate", type number}}),
    extractCurrency = Table.AddColumn(chgInitialTypes, "currency", each Text.BeforeDelimiter([currencyMonth], "-"), type text),
    extractDate = Table.AddColumn(extractCurrency, "exchDate", each Text.AfterDelimiter([currencyMonth], "-"), type text),
    chgExchDateType = Table.TransformColumnTypes(extractDate,{{"exchDate", type date}}),
    groupCurrency = Table.Group(chgExchDateType, {"currency"}, {{"data", each _, type table [currencyMonth=nullable text, exchRate=nullable number, currency=text, exchDate=nullable date]}}),
    addMaxExchDate = Table.AddColumn(groupCurrency, "maxExchDate", each Table.Max(Table.SelectRows([data], each [exchRate] <> 0), "exchDate")[exchRate]),
    expandMaxExchDate = Table.ExpandTableColumn(addMaxExchDate, "data", {"currencyMonth", "exchRate", "exchDate"}, {"currencyMonth", "exchRate", "exchDate"})
in
    expandMaxExchDate

 

Summary:

1) Split out currency and date portions of 'CAD-8-2022' column into their own columns.

2) Convert the new '8-2022' column to date format.

3) Group table on new 'CAD' column, adding an 'All Rows' aggregator.

4) Create a custom column that grabs the exchange rate for the latest date where the rate isn't zero.

5) Expand all rows back out again

 

Output:

BA_Pete_0-1664374689217.png

 

From here, it's up to you how you want to handle it. You could just create a new column with 'if rate = 0 then [yourNewRateColumn] etc. or do a conditional replace on the old column etc.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

2 REPLIES 2
BA_Pete
Super User
Super User

Hi @Xilalus ,

 

Paste this over the defult code in Advanced Editor to follow my steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnZ00TXXNTIwMlLSUTI30zM2UYrVgQhbIAkbmcOFLWHCBnAhQwMsYoZIYrEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [currencyMonth = _t, exchRate = _t]),
    chgInitialTypes = Table.TransformColumnTypes(Source,{{"currencyMonth", type text}, {"exchRate", type number}}),
    extractCurrency = Table.AddColumn(chgInitialTypes, "currency", each Text.BeforeDelimiter([currencyMonth], "-"), type text),
    extractDate = Table.AddColumn(extractCurrency, "exchDate", each Text.AfterDelimiter([currencyMonth], "-"), type text),
    chgExchDateType = Table.TransformColumnTypes(extractDate,{{"exchDate", type date}}),
    groupCurrency = Table.Group(chgExchDateType, {"currency"}, {{"data", each _, type table [currencyMonth=nullable text, exchRate=nullable number, currency=text, exchDate=nullable date]}}),
    addMaxExchDate = Table.AddColumn(groupCurrency, "maxExchDate", each Table.Max(Table.SelectRows([data], each [exchRate] <> 0), "exchDate")[exchRate]),
    expandMaxExchDate = Table.ExpandTableColumn(addMaxExchDate, "data", {"currencyMonth", "exchRate", "exchDate"}, {"currencyMonth", "exchRate", "exchDate"})
in
    expandMaxExchDate

 

Summary:

1) Split out currency and date portions of 'CAD-8-2022' column into their own columns.

2) Convert the new '8-2022' column to date format.

3) Group table on new 'CAD' column, adding an 'All Rows' aggregator.

4) Create a custom column that grabs the exchange rate for the latest date where the rate isn't zero.

5) Expand all rows back out again

 

Output:

BA_Pete_0-1664374689217.png

 

From here, it's up to you how you want to handle it. You could just create a new column with 'if rate = 0 then [yourNewRateColumn] etc. or do a conditional replace on the old column etc.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




Thank you Pete, this is awesome. I was missing the group and max magic 🙂

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors