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
Jorge_Lacerda
Regular Visitor

Calculated column in a table in Power BI project

Goal: Calculate the Forecast and Sales = sum of the current month, previous month, and next month.

1 -  This calculation must consider that we can have different products in the dataset, and this calculation needs to be done by the product.

2 - It is needed to consider in the dataset there might be some months missing (without sales and forecast) but the formula should consider the missing months as "0".

Example: First line of the sample dataset "Product 2". As we don't have Jan.2022, it is only the sum of Feb and Mar/2022.

3 - I need this calculation to be done inside the table (as a column calculated) for specific reasons of the business, working in an aggregation way directly in a measure, won't work, because obviously one product will offset another one  (in a DEVIATION = ABS (Sales (Quarter) - Forecast (Quarter)) column that I intend to calculate later) and it is not a result expected using measures.Sample Table.png

 

2 ACCEPTED SOLUTIONS
Jorge_Lacerda
Regular Visitor

To make it easier, I included the table in values over here. Thanks!

 

Start of MonthSKUIDForecastSales Forecast
(Quarter)
Sales
(Quarter)
01-Jan-22Product 16500750 6,9501,025
01-Feb-22Product 1450275 7,3302,026
01-Mar-22Product 13801001 1,5051,276
01-Apr-22Product 1675  1,0781,124
01-Jul-22Product 123122.5 8,78620,757
01-Aug-22Product 1808820634 69,71644,608
01-Sep-22Product 161604.523851.5 112,10186,744
01-Oct-22Product 142408.542258 139,56498,266
01-Nov-22Product 13555132156.5 91,47084,792
01-Dec-22Product 11351010377 49,23642,602
01-Jan-23Product 117568 13,93311,259
01-Feb-23Product 1247.5814 803882
01-Mar-23Product 1380  1,1431,329
01-Apr-23Product 1515515 1,580838
01-May-23Product 1685322.5 5,1353,538
01-Jun-23Product 139352700 4,9703,023
01-Jul-23Product 1350  24,8662,700
01-Aug-23Product 120581  57,4860
01-Sep-23Product 136555  94,2490
01-Oct-23Product 137113  127,3840
01-Nov-23Product 153716  91,2040
01-Dec-23Product 1375  54,0910
01-Feb-22Product 2900550 1,6602,552
01-Mar-22Product 27602002 3,0102,552
01-Apr-22Product 21350  2,1562,247
01-Jul-22Product 246245 17,57241,513
01-Aug-22Product 21617641268 139,43189,216
01-Sep-22Product 212320947703 224,202173,487
01-Oct-22Product 28481784516 279,128196,532
01-Nov-22Product 27110264313 182,939169,583
01-Dec-22Product 22702020754 98,47285,203
01-Jan-23Product 2350136 27,86522,518
01-Feb-23Product 24951628 1,6051,764
01-Mar-23Product 2760  2,2852,658
01-Apr-23Product 210301030 3,1601,675
01-May-23Product 21370645 10,2707,075
01-Jun-23Product 278705400 9,9406,045
01-Jul-23Product 2700  49,7325,400
01-Aug-23Product 241162  114,9720
01-Sep-23Product 273110  188,4980
01-Oct-23Product 274226  254,7680
01-Nov-23Product 2107432  182,4080
01-Dec-23Product 2750  108,1820

View solution in original post

HI @Jorge_Lacerda,

I'd like to suggest you to extract the current SKUID and date as condition to filter on table records and get result:

Forecast(quarter)=
CALCULATE (
    SUM ( Table1[Forecast] ),
    FILTER (
        Table1,
        VAR currDate =
            EARLIER ( Table1[Start of Month] )
        RETURN
            [SKUID] = EARLIER ( Table1[SKUID] )
                && [Start of Month]
                    >= DATE ( YEAR ( currDate ), MONTH ( currDate ) - 1, DAY ( currDate ) )
                && [Start of Month]
                    <= DATE ( YEAR ( currDate ), MONTH ( currDate ) + 1, DAY ( currDate ) )
    )
)

Sales(quarter) =
VAR _calendar =
    FILTER (
        CALENDAR (
            DATE ( YEAR ( Table1[Start of Month] ), MONTH ( Table1[Start of Month] ) - 1, 1 ),
            DATE ( YEAR ( Table1[Start of Month] ), MONTH ( Table1[Start of Month] ) + 1, 1 )
        ),
        DAY ( [Date] ) = 1
    )
VAR temp =
    ADDCOLUMNS (
        _calendar,
        "SKUID", Table1[SKUID],
        "Rolling",
            LOOKUPVALUE (
                Table1[Sales],
                Table1[SKUID], [SKUID],
                Table1[Start of Month], [Date],
                0
            )
    )
RETURN
    SUMX ( temp, [Rolling] ) + 0

For the not existed date ranges, I create a temp calendar table with complete ranges and manually fill these blank parts to zero.

1.png

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

3 REPLIES 3
Jorge_Lacerda
Regular Visitor

To make it easier, I included the table in values over here. Thanks!

 

Start of MonthSKUIDForecastSales Forecast
(Quarter)
Sales
(Quarter)
01-Jan-22Product 16500750 6,9501,025
01-Feb-22Product 1450275 7,3302,026
01-Mar-22Product 13801001 1,5051,276
01-Apr-22Product 1675  1,0781,124
01-Jul-22Product 123122.5 8,78620,757
01-Aug-22Product 1808820634 69,71644,608
01-Sep-22Product 161604.523851.5 112,10186,744
01-Oct-22Product 142408.542258 139,56498,266
01-Nov-22Product 13555132156.5 91,47084,792
01-Dec-22Product 11351010377 49,23642,602
01-Jan-23Product 117568 13,93311,259
01-Feb-23Product 1247.5814 803882
01-Mar-23Product 1380  1,1431,329
01-Apr-23Product 1515515 1,580838
01-May-23Product 1685322.5 5,1353,538
01-Jun-23Product 139352700 4,9703,023
01-Jul-23Product 1350  24,8662,700
01-Aug-23Product 120581  57,4860
01-Sep-23Product 136555  94,2490
01-Oct-23Product 137113  127,3840
01-Nov-23Product 153716  91,2040
01-Dec-23Product 1375  54,0910
01-Feb-22Product 2900550 1,6602,552
01-Mar-22Product 27602002 3,0102,552
01-Apr-22Product 21350  2,1562,247
01-Jul-22Product 246245 17,57241,513
01-Aug-22Product 21617641268 139,43189,216
01-Sep-22Product 212320947703 224,202173,487
01-Oct-22Product 28481784516 279,128196,532
01-Nov-22Product 27110264313 182,939169,583
01-Dec-22Product 22702020754 98,47285,203
01-Jan-23Product 2350136 27,86522,518
01-Feb-23Product 24951628 1,6051,764
01-Mar-23Product 2760  2,2852,658
01-Apr-23Product 210301030 3,1601,675
01-May-23Product 21370645 10,2707,075
01-Jun-23Product 278705400 9,9406,045
01-Jul-23Product 2700  49,7325,400
01-Aug-23Product 241162  114,9720
01-Sep-23Product 273110  188,4980
01-Oct-23Product 274226  254,7680
01-Nov-23Product 2107432  182,4080
01-Dec-23Product 2750  108,1820

HI @Jorge_Lacerda,

I'd like to suggest you to extract the current SKUID and date as condition to filter on table records and get result:

Forecast(quarter)=
CALCULATE (
    SUM ( Table1[Forecast] ),
    FILTER (
        Table1,
        VAR currDate =
            EARLIER ( Table1[Start of Month] )
        RETURN
            [SKUID] = EARLIER ( Table1[SKUID] )
                && [Start of Month]
                    >= DATE ( YEAR ( currDate ), MONTH ( currDate ) - 1, DAY ( currDate ) )
                && [Start of Month]
                    <= DATE ( YEAR ( currDate ), MONTH ( currDate ) + 1, DAY ( currDate ) )
    )
)

Sales(quarter) =
VAR _calendar =
    FILTER (
        CALENDAR (
            DATE ( YEAR ( Table1[Start of Month] ), MONTH ( Table1[Start of Month] ) - 1, 1 ),
            DATE ( YEAR ( Table1[Start of Month] ), MONTH ( Table1[Start of Month] ) + 1, 1 )
        ),
        DAY ( [Date] ) = 1
    )
VAR temp =
    ADDCOLUMNS (
        _calendar,
        "SKUID", Table1[SKUID],
        "Rolling",
            LOOKUPVALUE (
                Table1[Sales],
                Table1[SKUID], [SKUID],
                Table1[Start of Month], [Date],
                0
            )
    )
RETURN
    SUMX ( temp, [Rolling] ) + 0

For the not existed date ranges, I create a temp calendar table with complete ranges and manually fill these blank parts to zero.

1.png

Regards,
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

@Xiaoxin Sheng, Thanks so much for your help!

 

The only thing I would like to ask you is if you have any technique to make the table with all dates of the calendar automatically from this dataset because it will be fundamental to get those measures calculated in line that don't have sales or forecast in a certain month but eventually have some numbers in previous or in one-month forward.

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.