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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
URobert
Frequent Visitor

if statment based on 3 tables link with column date

Hi,

I have the below situation and i would need your help.

 

I have 3 tables and i want to create an if statment to calculate based on date column and customer.

Table 1:

URobert_2-1593499854772.png

Table 2

URobert_3-1593499879353.png

Table 3

URobert_4-1593499906403.png

 

I want to calculate for each customer based on date :

 

Point 1. -> formula A = table 1 - table 2 (on date and customer level)

URobert_5-1593499932179.png

Point 2:  Formula B = if(formula A < 0 ; formula A * (price 1 - price 3) ; formula A * (price 1 - price 2))

URobert_6-1593499962622.png

 

Thank you.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @URobert,

You can extract date and customer value from raw table to create new calculate tables, then you can use them as the category to write measure formulas to lookup corresponded values based on its category fields.

Calculate tables:

Calendar = 
DISTINCT ( UNION ( ALL ( C1[Data] ), ALL ( C2[Data] ), ALL ( P1[Data] ) ) )

Customer =
DISTINCT ( UNION ( ALL ( C1[Customer name] ), ALL ( C2[Customer name] ) ) )

Measures:

C1Value = 
CALCULATE (
    SUM ( C1[Value] ),
    FILTER (
        ALLSELECTED ( C1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [Customer name] IN VALUES ( Customer[Customer name] )
    )
)

C2Value = 
CALCULATE (
    SUM ( C2[Value] ),
    FILTER (
        ALLSELECTED ( C2 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [Customer name] IN VALUES ( Customer[Customer name] )
    )
)

P1 = 
CALCULATE (
    SUM ( P1[Value] ),
    FILTER (
        ALLSELECTED ( P1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [price] = "price 1"
    )
)

P2 = 
CALCULATE (
    SUM ( P1[Value] ),
    FILTER (
        ALLSELECTED ( P1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [price] = "price 2"
    )
)

P3 = 
CALCULATE (
    SUM ( P1[Value] ),
    FILTER (
        ALLSELECTED ( P1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [price] = "price 3"
    )
)

Diff = 
[C1Value] - [C2Value]

Result = 
IF ( [Diff] < 0, [Diff] * ( [P1] - [P3] ), [Diff] * ( [P1] - [P2] ) )

15.png

Regards,

Xiaoxin Sheng

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@URobert , this not the best data structure. First, unpivot tables and then create common date and customer dimensions. Then you should able to perform these calculations

https://radacad.com/pivot-and-unpivot-with-power-bi
Transpose : https://yodalearning.com/tutorials/power-query-helps-transposing-data/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

image.pngimage.pngimage.pngimage.pnghi,

 

I've unpivot the columns tables, but my issue is how i could calculate an If statment based on tables link only by dates.

Table 1

 

DataCustomer nameValue
01.01.2020Customer A0,80
01.01.2020Customer B0,10
01.01.2020Customer C0,61
02.01.2020Customer A0,51
02.01.2020Customer B0,82
02.01.2020Customer C1,17
03.01.2020Customer A0,59
03.01.2020Customer B0,66
03.01.2020Customer C1,08
04.01.2020Customer A0,72
04.01.2020Customer B0,37
04.01.2020Customer C0,72

 

Table 2

DataCustomer nameValue
01.01.2020Customer A0,00
01.01.2020Customer B0,80
01.01.2020Customer C0,10
02.01.2020Customer A0,72
02.01.2020Customer B0,51
02.01.2020Customer C0,82
03.01.2020Customer A0,56
03.01.2020Customer B0,59
03.01.2020Customer C0,66
04.01.2020Customer A0,27
04.01.2020Customer B0,72
04.01.2020Customer C0,37

 

table 3

DatapriceValue
01.01.2020price 1200
01.01.2020price 2100
01.01.2020price 3300
02.01.2020price 1200
02.01.2020price 2100
02.01.2020price 3300
03.01.2020price 1200
03.01.2020price 2100
03.01.2020price 3300
04.01.2020price 1200
04.01.2020price 2100
04.01.2020price 3300

 

Table 3 has only dates.

I nees to link all this 3 tables and to create the below formulas :

       Formula A = table 1 - table 2 (on date and customer level)

       Formula B = if(formula A < 0 ; formula A * (price 1 - price 3) ; formula A * (price 1 - price 2))

 

And to be able to create this visualization :

 

dataCustomer table 1 valuetable 2 valueprice 1price 2price 3Formula AFormula B
01.01.2020Customer A0,800,002001003000,8079,60
01.01.2020Customer B0,100,80200100300-0,7069,60
01.01.2020Customer C0,610,102001003000,5150,91
02.01.2020Customer A0,510,72200100300-0,2120,94
02.01.2020Customer B0,820,512001003000,3130,94
02.01.2020Customer C1,170,822001003000,3434,38
03.01.2020Customer A0,590,562001003000,032,92
03.01.2020Customer B0,660,592001003000,077,08
03.01.2020Customer C1,080,662001003000,4242,24
04.01.2020Customer A0,720,272001003000,4545,02
04.01.2020Customer B0,370,72200100300-0,3535,02
04.01.2020Customer C0,720,372001003000,3635,62

 

Anonymous
Not applicable

HI @URobert,

You can extract date and customer value from raw table to create new calculate tables, then you can use them as the category to write measure formulas to lookup corresponded values based on its category fields.

Calculate tables:

Calendar = 
DISTINCT ( UNION ( ALL ( C1[Data] ), ALL ( C2[Data] ), ALL ( P1[Data] ) ) )

Customer =
DISTINCT ( UNION ( ALL ( C1[Customer name] ), ALL ( C2[Customer name] ) ) )

Measures:

C1Value = 
CALCULATE (
    SUM ( C1[Value] ),
    FILTER (
        ALLSELECTED ( C1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [Customer name] IN VALUES ( Customer[Customer name] )
    )
)

C2Value = 
CALCULATE (
    SUM ( C2[Value] ),
    FILTER (
        ALLSELECTED ( C2 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [Customer name] IN VALUES ( Customer[Customer name] )
    )
)

P1 = 
CALCULATE (
    SUM ( P1[Value] ),
    FILTER (
        ALLSELECTED ( P1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [price] = "price 1"
    )
)

P2 = 
CALCULATE (
    SUM ( P1[Value] ),
    FILTER (
        ALLSELECTED ( P1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [price] = "price 2"
    )
)

P3 = 
CALCULATE (
    SUM ( P1[Value] ),
    FILTER (
        ALLSELECTED ( P1 ),
        [Data] IN VALUES ( 'Calendar'[Data] )
            && [price] = "price 3"
    )
)

Diff = 
[C1Value] - [C2Value]

Result = 
IF ( [Diff] < 0, [Diff] * ( [P1] - [P3] ), [Diff] * ( [P1] - [P2] ) )

15.png

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors