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
Anonymous
Not applicable

Create measures based on values from two tables

Hello everyone, I have done many tries but I am stuck here and could use your help 🙂

 

I have two tables, one presenting sales of product and another one presenting prediction of sales :

Sales

CategoryColourday_of_SalesSoldID
ChairBlue310ChairBlue3
ChairBlue220ChairBlue2
ChairBlue110ChairBlue1
ChairRed210ChairRed2
ChairRed315ChairRed3
CarBlue15CarBlue1
CarGreen220CarGreen2
CarGreen420CarGreen4

Prediction

CategoryColourDay_of_Predictionday_of_SalesPredictionID
ChairBlue3310ChairBlue3
ChairBlue3210ChairBlue2
ChairBlue315ChairBlue1
ChairBlue2210ChairBlue2
ChairBlue2130ChairBlue1
ChairBlue1110ChairBlue1
ChairRed3320ChairRed3
ChairRed1120ChairRed1
TableBlue3110TableBlue1
TableBlue325TableBlue2
TableBlue425TableBlue2

(day of sales represents the number of days before an event the product is sold, and day of prediction represents the number of  days before this event that the prediction is done - so day_of_prediction >= day_of_sales)


From these tables, I have create three others : Product_day (have all distinct combination of ID=Category&&Colour&&day of sales), day_of_prediction (have all the distinct day_of_prediction) and day_of_sales (have all the distinct day_of_sales) and organized them this way :

jlwork_0-1683901017081.png


From this I have created this matrix (summing prediction of sales per day of sales and day of prediction :

days of sales / days of prediction1234
1303015 
2 10155
3  30 


What I need is to create a measure that would allow me for every combination of day_of_sales and day_of prediction to have the future sales for this specific day of prediction but for the same and future day of sales.

 

The final results of this measure in a matrix would be (I have put the calculation between parenthesis) :

days of sales / days of prediction1234
1303015 
2 40 (10+30)30 (15+15)5
3  60 (30+15+15) 


Any help or advice would be highly appreciated !

Thanks

1 ACCEPTED SOLUTION

Hi, @Anonymous 

Thanks for your quick response !

You can try to use this dax code:

Measure = var _t =ADDCOLUMNS( CROSSJOIN( ALLSELECTED('day_of_sales'[day_of_Sales]) , ALLSELECTED('day_of_prediction'[Day_of_Prediction])) , "value" ,  CALCULATE( SUM('Prediction'[Prediction])))

var _cur_sales=MAX('Prediction'[day_of_Sales])

var _cur_pre = MAX('Prediction'[Day_of_Prediction])

var _t2 =  FILTER(_t,  [Day_of_Prediction]=_cur_pre && [day_of_Sales]<=_cur_sales)

return

IF(ISBLANK(_cur_pre),BLANK(),SUMX(_t2,[value]))

 

The result is as follows:

vyueyunzhmsft_0-1684286254949.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya 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

4 REPLIES 4
v-yueyunzh-msft
Community Support
Community Support

Hi , @Anonymous 

Thanks for your sample data first~

According to your description, you want to get the sum of the final Matrix visual in your image , and it seems the value is all from the 'Prediction' Table.

Here are the steps you can refer to :
(1)This is my test data:

vyueyunzhmsft_0-1684120888573.png

(2)We can create a measure like this:

Measure = var _t =ADDCOLUMNS( CROSSJOIN( ALLSELECTED('day_of_sales'[day_of_Sales]) , ALLSELECTED('day_of_prediction'[Day_of_Prediction])) , "value" ,  CALCULATE( SUM('Prediction'[Prediction])))
var _cur_sales= MAX('day_of_sales'[day_of_Sales])
var _cur_pre = MAX('Prediction'[Day_of_Prediction])
var _t2 =  FILTER(_t,  [Day_of_Prediction]=_cur_pre && [day_of_Sales]<=_cur_sales)
return
SUMX(_t2,[value])

 

Then we can put it on the visual and we can get this result as follows:

vyueyunzhmsft_1-1684120925207.png

 

 

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Anonymous
Not applicable

Hi  v-yueyunzh-msft ! Thank your for your answer !

 

It seems to work well excepted in the case when there are days_of_prediction=0.

For instance if we take this data as the prediction table: 

CategoryColourDay_of_PredictionSum of day_of_SalesID
ChairBlue00ChairBlue1
ChairBlue10ChairBlue1
ChairBlue11ChairBlue2
ChairBlue20ChairBlue1
ChairBlue21ChairBlue2
ChairBlue22ChairBlue3
ChairRed00ChairRed1
ChairRed22ChairRed3
TableBlue20TableBlue1
TableBlue21TableBlue2
TableBlue31TableBlue2


The results are : 

jlwork_1-1684263461561.png

 


We can see values from days_of_prediction=0 go to cases when it is not supposed to go. 

Do you know how that can be solved ?

 

Thank you !

Hi, @Anonymous 

Thanks for your quick response !

You can try to use this dax code:

Measure = var _t =ADDCOLUMNS( CROSSJOIN( ALLSELECTED('day_of_sales'[day_of_Sales]) , ALLSELECTED('day_of_prediction'[Day_of_Prediction])) , "value" ,  CALCULATE( SUM('Prediction'[Prediction])))

var _cur_sales=MAX('Prediction'[day_of_Sales])

var _cur_pre = MAX('Prediction'[Day_of_Prediction])

var _t2 =  FILTER(_t,  [Day_of_Prediction]=_cur_pre && [day_of_Sales]<=_cur_sales)

return

IF(ISBLANK(_cur_pre),BLANK(),SUMX(_t2,[value]))

 

The result is as follows:

vyueyunzhmsft_0-1684286254949.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Anonymous
Not applicable

Thank you !

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.