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
cscollier2
New Member

Merging columns from two tables

Hello, community I am reaching out with a question that has stumped me. I am attempting to merge two tables in power query to bring in additional data. I am going to let our the scenario as best as I can. Unfortunately, I cannot share the data as it is proprietary to my work. 

 

First Table - The first table contains several million rows of sales data for each transaction that is processed at work. This contains the date, dimension data, and the amount of the sale

 

Second Table - The second table contains discount rates that need to be applied to the sales transaction. However, the discount rates have a time frame when they are valid. Such as 10% from 04/2022 to 07/2022 and 15% from 08/2022 to 11/2022. 

 

What I am attempting to do is merge the correct discount percentages with the correct sales transaction dates. Such as any sales that occurred from 04/2022 to 07/2022 bring in a 10% value and any sales from 08/2022 to 11/2022 bring in a discount value of 15%. The problem that I run into is when merging the first table with the second on a dimension value it ends up duplicating the discount percentages because it brings in the 10% and the 15%. 

 

Can someone assist with this issue!

1 REPLY 1
BA_Pete
Super User
Super User

Hi @cscollier2 ,

 

I think you'll want to do this in DAX. Conditional merges in Power Query can be complicated and provide poor performance.

I'm normally totally against the use of DAX calculated columns, but the following is the most performant way of dealing with SCD tables I've found so far. I've got a 1M row table that I do this twice on, the second column referencing the first, and it's lightning-fast. However, I've not tested this on tables over 1M rows, so YMMV.

 

As a DAX calculated column on your Sales table:

..saleDiscount = 
CALCULATE(
    // Add a variable for each dimension that you need to match on
    VAR __departmentRow = VALUES(salesTable[department])
    VAR __salesDateRow = VALUES(salesTable[salesDate])
    RETURN
    MAXX(
        FILTER(
            discountSCDTable,
            // Match the different variables to the discountSCD table
            discountSCDTable[department] = __departmentRow
            && discountSCDTable[discountStartDate] <= __salesDateRow
            && discountSCDTable[discountEndDate] >= __salesDateRow
        ),
        discountSCDTable[discountRate]
    )
)

 

Pete

 



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

Proud to be a Datanaut!




Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

Top Solution Authors
Top Kudoed Authors