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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
IvoM
Frequent Visitor

Linear regression on UNION data

Hey all,
I have been trying to solve this for quite some time now. I would like a measure that calculate the linear regression. But here is a twist:
My data comes from two sources:
Fir source is actual real data from sytem that are calculated like this:

ForecastSixMSumQtyOORECLast3Month = 
VAR _EndDate = EOMONTH(TODAY(), -1)
VAR _StartDate = EOMONTH(_EndDate, -3) + 1
RETURN
SUMX(
SUMMARIZECOLUMNS(
    DimCalendar[YYYY-MM],
    KEEPFILTERS( FILTER( ALL( DimCalendar[Date] ), DimCalendar[Date] >= _StartDate && DimCalendar[Date] <= _EndDate )),
    "SumQtyOOREC", SUM(FactsTransactionOOREC[Qty])
),
IF(ISBLANK([SumQtyOOREC]),0,[SumQtyOOREC])
)

Second data source is forecast that is calculated like this:

ForecastSixMSumQtyNewestBackup =
SUMX(DimCalendar,
    CALCULATE(
        SUM(FactsSixMForecast[QtyPerDay]),
        FactsSixMForecast[BackUpOrder] IN {1,0}
    )
)

Now I have calculated table:

Test2 =
VAR _EndDate1 = EOMONTH(TODAY(), -1)
VAR _StartDate1 = EOMONTH(_EndDate1, -3) + 1
VAR _StartDate2 = EOMONTH(TODAY(), -1) + 1
VAR _EndDate2 = EOMONTH(_StartDate2, 2)
VAR CombinedData =
    UNION(
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate1 && DimCalendar[Date] <= _EndDate1
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", SUM(FactsTransactionOOREC[Qty])
        ),
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate2 && DimCalendar[Date] <= _EndDate2
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", [ForecastSixMSumQtyNewestBackup]
        )
    )
RETURN
    ADDCOLUMNS(
        CombinedData,
        "Rank", RANKX(CombinedData, [YYYY-MM], , ASC, DENSE),
        "Date", DATE(LEFT([YYYY-MM], 4), RIGHT([YYYY-MM], 2), 1)
    )

The column date is there only so I can create relationship between this table and DimCalendar[Date]
The result of this calculated table is this:

YYYY-MMQtyRankDate
2024-1169634101.11.2024 0:00:00
2024-1252778201.12.2024 0:00:00
2025-0190086301.01.2025 0:00:00
2025-0291059,9999999997401.02.2025 0:00:00
2025-0399472,3501.03.2025 0:00:00
2025-04106012,875601.04.2025 0:00:00

Then I have second calculated table:

Test1 =
VAR _EndDate1 = EOMONTH(TODAY(), -1)
VAR _StartDate1 = EOMONTH(_EndDate1, -3) + 1
VAR _StartDate2 = EOMONTH(TODAY(), -1) + 1
VAR _EndDate2 = EOMONTH(_StartDate2, 2)
VAR CombinedData =
    UNION(
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate1 && DimCalendar[Date] <= _EndDate1
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", SUM(FactsTransactionOOREC[Qty])
        ),
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate2 && DimCalendar[Date] <= _EndDate2
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", [ForecastSixMSumQtyNewestBackup]
        )
    )
VAR CombinedDataWithRank =
    ADDCOLUMNS(
        CombinedData,
        "Rank", RANKX(CombinedData, [YYYY-MM], , ASC, DENSE)
    )
RETURN
    LINESTX(CombinedDataWithRank, [Qty], [Rank], TRUE)

The result is this table:

Slope1InterceptStandardErrorSlope1StandardErrorInterceptCoefficientOfDeterminationStandardErrorFStatisticDegreesOfFreedomRegressionSumOfSquaresResidualSumOfSquares
9227,1792857142752545,40166666672666,8155860180410385,75457581530,74955535344673211156,089994796711,971593144632241489964657,4875497833375,888011

With all this I have this final calculation:

ForecastSixM-3+3TrendLine =    
    MAX(Test1[Intercept]) + Max(Test1[Slope1]) *MAX(Test2[Rank])

This gives me nice straight line showing the trend:

IvoM_0-1738755570145.png

Since this is quite a lot calculated tables, I wanted to make it bit more simple using one measure. So I come up with this measure:

TestForecastSixM-3+3TrendLine =
VAR _EndDate1 = EOMONTH(TODAY(), -1)
VAR _StartDate1 = EOMONTH(_EndDate1, -3) + 1
VAR _StartDate2 = EOMONTH(TODAY(), -1) + 1
VAR _EndDate2 = EOMONTH(_StartDate2, 2)
VAR CombinedData =
    UNION(
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate1 && DimCalendar[Date] <= _EndDate1
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", SUM(FactsTransactionOOREC[Qty])
        ),
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate2 && DimCalendar[Date] <= _EndDate2
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", [ForecastSixMSumQtyNewestBackup]
        )
    )
VAR CombinedDataWithRank =
    ADDCOLUMNS(
        CombinedData,
        "Rank", RANKX(CombinedData, [YYYY-MM], , ASC, DENSE)
    )
VAR RegressionResult =
    LINESTX(
        CombinedDataWithRank,
        [Qty], [Rank], TRUE
    )
VAR Intercept = MAXX(RegressionResult, [Intercept])
VAR Slope = MAXX(RegressionResult, [Slope1])
VAR CurrentRank = MAXX(CombinedDataWithRank, [Rank])
RETURN
    Intercept + Slope * CurrentRank

The only problem is that I can't  remove the context filter. So if I use this in table visual or my chart the result is that the LINESTX is calculated for every line and not for the whole CombinedDataWithRank variable.
So the result is this:

YYYY-MMForecastSixM-3+3TrendLineTestForecastSixM-3+3TrendLine
2024-1161772,5809569634
2024-1270999,7602452778
2025-0180226,9395290086
2025-0289454,1188191060
2025-0398681,298199472,3
2025-04107908,4774106012,875
IvoM_1-1738755920230.png


If someone could help me to solve this issue that would be great. Or confirm that the aproach with calculated tables is my best option.
Thanks to anyone who replies 😊

2 REPLIES 2
Anonymous
Not applicable

Hi @IvoM ,

 

Please try using the REMOVEFILTERS function in the measure to remove the context filter.

TestForecastSixM-3+3TrendLine =
VAR _EndDate1 = EOMONTH(TODAY(), -1)
VAR _StartDate1 = EOMONTH(_EndDate1, -3) + 1
VAR _StartDate2 = EOMONTH(TODAY(), -1) + 1
VAR _EndDate2 = EOMONTH(_StartDate2, 2)
VAR CombinedData = 
    UNION(
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate1 && DimCalendar[Date] <= _EndDate1
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", SUM(FactsTransactionOOREC[Qty])
        ),
        SUMMARIZECOLUMNS(
            DimCalendar[YYYY-MM],
            FILTER(
                ALL(DimCalendar[Date]),
                DimCalendar[Date] >= _StartDate2 && DimCalendar[Date] <= _EndDate2
            ),
            FILTER(
                ALL(DimPartProductFamily),
                DimPartProductFamily[FamilyType] = "Výroba"
            ),
            "Qty", [ForecastSixMSumQtyNewestBackup]
        )
    )
VAR CombinedDataWithRank = 
    ADDCOLUMNS(
        CombinedData,
        "Rank", RANKX(CombinedData, [YYYY-MM], , ASC, DENSE)
    )
VAR AllCombinedDataWithRank = 
    CALCULATETABLE(
        CombinedDataWithRank,
        REMOVEFILTERS(DimCalendar),
        REMOVEFILTERS(DimPartProductFamily)
    )
VAR RegressionResult = 
    LINESTX(
        AllCombinedDataWithRank,
        [Qty], [Rank], TRUE
    )
VAR Intercept = MAXX(RegressionResult, [Intercept])
VAR Slope = MAXX(RegressionResult, [Slope1])
VAR CurrentRank = MAXX(CombinedDataWithRank, [Rank])
RETURN
    Intercept + Slope * CurrentRank

 

Please try it and let me know if it helps, and if it doesn't, please provide a Power BI Desktop file in progress (with sensitive information removed) that fully covers your issue or question in a usable format (not a screenshot). You can upload the PBIX file to a cloud storage service such as OneDrive, Google Drive (set up public access), SharePoint, or a Github repository, and then share the URL of the file.

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Hey @Anonymous 

Thank you for your quick response! I tried your proposed solution, but unfortunately, it doesn’t change the result of the measure.

The issue seems to be that every measure I try is still being evaluated row by row.

Here is the link to the PBIX file—I’ve removed all unnecessary information. Even though it's a completely new file, the behavior remains the same.
Test1.pbix 

Thanks again for your help!


Edit: The table aproach also disable filtering based on Contract, PartNo and PartProductFamily which is not ideal.



Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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