cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
eyesem
Helper I
Helper I

Relate tables on several columns in a DAX measure

Hi

 

I have these two unrelated tables:

Price_table

Client       Date_start_price    Date_end_price   Price
101/01/202131/01/20215
101/02/202131/03/20216
201/01/202130/06/20217
201/07/202131/12/20214

 

Discount_table

Client   Date_start_discount   Date_end_discount   Discount
101/02/202128/02/20211
101/03/202131/03/20214
201/03/202130/04/20212
201/06/202131/9/20213

 

I will always filter the Price table for an specific date (ie start<01/2021 && end >01/02/2021) and I need to add a measure with the discount applying on that date:

 

Price_Table (filtered start<01/2021 && end >01/02/2021)

Client   Price   @Discount
151
270

 

¿How can I get that?

 

Thanks.

1 ACCEPTED SOLUTION
v-robertq-msft
Community Support
Community Support

Hi, @

According to your description, I can roughly understand what you want to get, you want to filter the Price table for a specific date and get the discount based on the start and end date, right? I think you can achieve this using measures and visual filter in Power BI, you can try my steps:

  1. Create a Calendar table for the date slicer:
Date = CALENDARAUTO()
  1. Create two measures:
Flag =

var _selectperiod=

SELECTCOLUMNS(ALLSELECTED('Date'),"1",[Date])

return

IF(

    MAX('Price_table'[Date_start_price]) in _selectperiod||

    MAX('Price_table'[Date_end_price]) in _selectperiod,

    1,0

)
Dicsount1 =

var _value=

CALCULATE(

    MAX('Discount_table'[Discount]),

    FILTER(

        ALL(Discount_table),

        [Date_start_discount]<=MAX([Date_start_price])&&

        [Date_end_discount]>=MAX([Date_end_price])&&

        [Client]=MAX([Client])))

return

IF(_value=BLANK(),0,_value)
  1. Create a Slicer and place ‘Date’[Date], and a table chart then place it like this:

v-robertq-msft_0-1614651650476.png

 

  1. Then add a filter to this filter, like this:

v-robertq-msft_1-1614651650488.png

 

And you can get what you want.

You can download my test pbix file here

 

If this result is not what you want, you can post some sample data(without sensitive data) and your expected result so that we can help you in advance.

How to Get Your Question Answered Quickly 

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-robertq-msft
Community Support
Community Support

Hi, @

According to your description, I can roughly understand what you want to get, you want to filter the Price table for a specific date and get the discount based on the start and end date, right? I think you can achieve this using measures and visual filter in Power BI, you can try my steps:

  1. Create a Calendar table for the date slicer:
Date = CALENDARAUTO()
  1. Create two measures:
Flag =

var _selectperiod=

SELECTCOLUMNS(ALLSELECTED('Date'),"1",[Date])

return

IF(

    MAX('Price_table'[Date_start_price]) in _selectperiod||

    MAX('Price_table'[Date_end_price]) in _selectperiod,

    1,0

)
Dicsount1 =

var _value=

CALCULATE(

    MAX('Discount_table'[Discount]),

    FILTER(

        ALL(Discount_table),

        [Date_start_discount]<=MAX([Date_start_price])&&

        [Date_end_discount]>=MAX([Date_end_price])&&

        [Client]=MAX([Client])))

return

IF(_value=BLANK(),0,_value)
  1. Create a Slicer and place ‘Date’[Date], and a table chart then place it like this:

v-robertq-msft_0-1614651650476.png

 

  1. Then add a filter to this filter, like this:

v-robertq-msft_1-1614651650488.png

 

And you can get what you want.

You can download my test pbix file here

 

If this result is not what you want, you can post some sample data(without sensitive data) and your expected result so that we can help you in advance.

How to Get Your Question Answered Quickly 

Thank you very much!

 

Best Regards,

Community Support Team _Robert Qin

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Thanks I did some slight changes and it works wonderfully!

negi007
Super User II
Super User II

@eyesem 

in this case, there are few things that you will have to do

1) create a date table which will be used to filter data by date and link two tables

Data_Tab = CALENDARAUTO()

 

2) create one more table that will have unique values of ClientIDs from Pricing and Discount table

Client = DISTINCT(UNION(DISTINCT(Discount_Tab[Client ]),DISTINCT(Price_Tab[Client ])))
 
3) then you create relationship between tables like below
 
negi007_1-1614431199898.png

 

 

4) below is the visual you want to have here

negi007_2-1614431216432.png

 

 

I have attached pbix file as well

thanks. let me know if helps you




Did I answer your question? Mark my post as a solution!
Appreciate your Kudos


Proud to be a Super User!

Follow me here


Thank you

Thank you @negi007 

 

This works fine for the months where the price starts, but not for those in between. For example August should display a price of 4 and discount of 3 (client 2), but there is no start date for august in the tables, that is the tricky thing here.

Helpful resources

Announcements
PBI User Groups

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group!

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are You Ready?

Test your skills now with the Cloud Skills Challenge.

Top Solution Authors