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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
manojsv16
Helper II
Helper II

DAX to create cumulative price table

Hello,

 

I have been working on to create a cumulative price table shown below. 

 

I wanted to calculate cumulative price based on the below calculated columns ( for ex Cumulative Price 461.8 = SUM(Price 2018Q1) Calculated column). I am struck with how to pivot the calculate price to the corresponding year and quarter based on the calculated column name. 

 I would appreciate any suggestions/help in getting achieve the below output table. I have also provided the input table and DAX formula for calculated column

 

Please let me know if you need any further information.

 

YearQuarterCumulative Price
2018Q1461.18
2018Q2386
2018Q3317.3
2018Q4292.6
2019Q1261.775
2019Q2195.25
2019Q3176.75
2019Q4158.05
2020Q1150.25
2020Q2131.75
2020Q397.55
2020Q428.375

 

Following fields are the acutal input data

ID, Name, Year, Quarter Price

 

Remaining fields are calculated columns

 

Target  = SWITCH(
CONCATENATE(Price_data[Year],Price_data[Quarter]),
"2018Q1",1,
"2018Q2",2,
"2018Q3",3,
"2018Q4",4,
"2019Q1",5,
"2019Q2",6,
"2019Q3",7,
"2019Q4",8,
"2020Q1",9,
"2020Q2",10,
"2020Q3",11,
"2020Q4",12
)
Price 2018 Q1 =
IF(Price_data[Target] >= 1,
        IF(Price_data[Price] > 0,
           DIVIDE(Price_data[Price],4),0),
        0)
Price 2018 Q2 =
IF(Price_data[Target] >= 2,
        IF(Price_data[Price] > 0,
           DIVIDE(Price_data[Price],4),0),
        0)

 

 

 

Input Data

ID            Name      Year       Quarter Price      Target Price2018Q1 Price 2018Q2 Price 2018 Q3 Price 2018 Q4 Price 2019 Q1 

1Asset 12018Q150.4112.600000000000
2Asset 22018Q1250162.500000000000
3Asset 32018Q2250262.562.50000000000
4Asset 42018Q22526.256.250000000000
5Asset 52018Q389322.2522.2522.25000000000
6Asset 62018Q31032.52.52.5000000000
7Asset 72018Q478.23419.557519.557519.557519.557500000000
8Asset 82018Q445411.2511.2511.2511.2500000000
9Asset 92019Q1250562.562.562.562.562.50000000
10Asset 102019Q11553.753.753.753.753.750000000
11Asset 112019Q21563.753.753.753.753.753.75000000
12Asset 122019Q2606151515151515000000
13Asset 132019Q31573.753.753.753.753.753.753.7500000
14Asset 142019Q36071515151515151500000
15Asset 152019Q41583.753.753.753.753.753.753.753.750000
16Asset 162019Q41583.753.753.753.753.753.753.753.750000
17Asset 172020Q11593.753.753.753.753.753.753.753.753.75000
18Asset 182020Q1609151515151515151515000
19Asset 192020Q234.54108.6358.6358.6358.6358.6358.6358.6358.6358.6358.63500
20Asset 202020Q2102.341025.58525.58525.58525.58525.58525.58525.58525.58525.58525.58500
21Asset 212020Q3189.561147.3947.3947.3947.3947.3947.3947.3947.3947.3947.3947.390
22Asset 222020Q387.231121.807521.807521.807521.807521.807521.807521.807521.807521.807521.807521.80750
23Asset 232020Q494.231223.557523.557523.557523.557523.557523.557523.557523.557523.557523.557523.557523.5575
24Asset 242020Q419.2124.84.84.84.84.84.84.84.84.84.84.84.8

 

1 ACCEPTED SOLUTION
v-yuezhe-msft
Employee
Employee

@manojsv16,

Create all the calculated columns in Power BI Desktop query editor.

For target:

=if Number.ToText([Year])&[Quarter]="2018Q1" then 1 else
if Number.ToText([Year])&[Quarter]="2018Q2" then 2 else
if Number.ToText([Year])&[Quarter]="2018Q3" then 3 else
if Number.ToText([Year])&[Quarter]="2018Q4" then 4 else
if Number.ToText([Year])&[Quarter]="2019Q1" then 5 else
if Number.ToText([Year])&[Quarter]="2019Q2" then 6 else
if Number.ToText([Year])&[Quarter]="2019Q3" then 7 else
if Number.ToText([Year])&[Quarter]="2019Q4" then 8 else
if Number.ToText([Year])&[Quarter]="2020Q1" then 9 else
if Number.ToText([Year])&[Quarter]="2020Q2" then 10 else
if Number.ToText([Year])&[Quarter]="2020Q3" then 11 else
12

1.PNG

For Price 2018 Q1:

=if [Target] >= 1 
then if [Price] > 0 then [Price]/4 else 0
else 0

2.PNG

For Price 2018 Q2:

=if [Target] >= 2 
then if [Price] > 0 then [Price]/4 else 0
else 0


In Query Editor, select all Price 201X QX columns, then unpivot these columns.
3.PNG

After you apply changes to Power BI report, create the following calculated columns in your table.

NewYear = MID('Price_data'[Attribute],7,4)
NewQr = RIGHT('Price_data'[Attribute],2)


Create table visual as below.
4.PNG

Regards,
Lydia

Community Support Team _ Lydia Zhang
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

1 REPLY 1
v-yuezhe-msft
Employee
Employee

@manojsv16,

Create all the calculated columns in Power BI Desktop query editor.

For target:

=if Number.ToText([Year])&[Quarter]="2018Q1" then 1 else
if Number.ToText([Year])&[Quarter]="2018Q2" then 2 else
if Number.ToText([Year])&[Quarter]="2018Q3" then 3 else
if Number.ToText([Year])&[Quarter]="2018Q4" then 4 else
if Number.ToText([Year])&[Quarter]="2019Q1" then 5 else
if Number.ToText([Year])&[Quarter]="2019Q2" then 6 else
if Number.ToText([Year])&[Quarter]="2019Q3" then 7 else
if Number.ToText([Year])&[Quarter]="2019Q4" then 8 else
if Number.ToText([Year])&[Quarter]="2020Q1" then 9 else
if Number.ToText([Year])&[Quarter]="2020Q2" then 10 else
if Number.ToText([Year])&[Quarter]="2020Q3" then 11 else
12

1.PNG

For Price 2018 Q1:

=if [Target] >= 1 
then if [Price] > 0 then [Price]/4 else 0
else 0

2.PNG

For Price 2018 Q2:

=if [Target] >= 2 
then if [Price] > 0 then [Price]/4 else 0
else 0


In Query Editor, select all Price 201X QX columns, then unpivot these columns.
3.PNG

After you apply changes to Power BI report, create the following calculated columns in your table.

NewYear = MID('Price_data'[Attribute],7,4)
NewQr = RIGHT('Price_data'[Attribute],2)


Create table visual as below.
4.PNG

Regards,
Lydia

Community Support Team _ Lydia Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.