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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Breitwieser
New Member

Calc Diff / Compare per share and holder in %

Hello everyone,
I have the following problem and haven't found a way to solve the "problem".
I have a matrix that receives a customer number via a Scliver MultiSelect. Based on these two customer numbers, the same products should now be compared based on the percentage and marked as over or underrated.

my matrix has the following structure "row Assetklasse;WKN column Portfolio Values Percentage;AssetDescription"

Breitwieser_0-1723143970222.png

 

One of this not working scripts:

 

 

 

PROD1 = 
VAR SelectedPortfolios = VALUES(PositionData[Portfolio])
VAR SelectedCount = COUNTROWS(SelectedPortfolios)

VAR Portfolio1 = 
    IF(
        SelectedCount = 2,
        FIRSTNONBLANK(SelectedPortfolios, PositionData[Portfolio])
    )

VAR Portfolio2 = 
    IF(
        SelectedCount = 2,
        LASTNONBLANK(SelectedPortfolios, PositionData[Portfolio])
    )

VAR Portfolio1Aggregated = 
    SUMMARIZE(
        FILTER(
            PositionData,
            PositionData[Portfolio] = Portfolio1
        ),
        PositionData[AssetName],
        "Portfolio1Percentage", MAX(PositionData[POS_PORTF_PERCENTAGE])
    )

VAR Portfolio2Aggregated = 
    SUMMARIZE(
        FILTER(
            PositionData,
            PositionData[Portfolio] = Portfolio2
        ),
        PositionData[AssetName],
        "Portfolio2Percentage", MAX(PositionData[POS_PORTF_PERCENTAGE])
    )

VAR CommonAssetNames = 
    INTERSECT(
        SELECTCOLUMNS(Portfolio1Aggregated, "AssetName", PositionData[AssetName]),
        SELECTCOLUMNS(Portfolio2Aggregated, "AssetName", PositionData[AssetName])
    )

VAR DifferencesTable = 
    ADDCOLUMNS(
        CommonAssetNames,
        "Difference",
        CALCULATE(
            MAXX(
                FILTER(Portfolio1Aggregated, PositionData[AssetName] = [AssetName]),
                [Portfolio1Percentage]
            ) -
            MAXX(
                FILTER(Portfolio2Aggregated, PositionData[AssetName] = [AssetName]),
                [Portfolio2Percentage]
            )
        )
    )

VAR AverageDifference = 
    AVERAGEX(DifferencesTable, [Difference])

RETURN
    IF(
        SelectedCount = 2,
        AverageDifference,
        BLANK()      )

 

 

 

2 REPLIES 2
Breitwieser
New Member

good morning and thanks for your answer!

 

There are still some errors in 

Breitwieser_0-1723188068995.png

 

"Portfolio1Aggregated / AssetName" could not be found and "Portfolio1Percentage" also could not be found / wrong type. But arg had to ref to an table - did not see or get ehat is the issue

hackcrr
Super User
Super User

@Breitwieser 

You can try the following DAX expression:

Diff_Percentage = 
VAR SelectedPortfolios = VALUES(PositionData[Portfolio])
VAR SelectedCount = COUNTROWS(SelectedPortfolios)

VAR Portfolio1 = 
    IF(
        SelectedCount = 2,
        FIRSTNONBLANK(SelectedPortfolios, PositionData[Portfolio])
    )

VAR Portfolio2 = 
    IF(
        SelectedCount = 2,
        LASTNONBLANK(SelectedPortfolios, PositionData[Portfolio])
    )

VAR Portfolio1Data = 
    FILTER(
        PositionData,
        PositionData[Portfolio] = Portfolio1
    )

VAR Portfolio2Data = 
    FILTER(
        PositionData,
        PositionData[Portfolio] = Portfolio2
    )

VAR Portfolio1Aggregated = 
    SUMMARIZE(
        Portfolio1Data,
        PositionData[AssetName],
        "Portfolio1Percentage", MAX(PositionData[POS_PORTF_PERCENTAGE])
    )

VAR Portfolio2Aggregated = 
    SUMMARIZE(
        Portfolio2Data,
        PositionData[AssetName],
        "Portfolio2Percentage", MAX(PositionData[POS_PORTF_PERCENTAGE])
    )

VAR CommonAssets = 
    INTERSECT(
        SELECTCOLUMNS(Portfolio1Aggregated, "AssetName", PositionData[AssetName]),
        SELECTCOLUMNS(Portfolio2Aggregated, "AssetName", PositionData[AssetName])
    )

VAR DifferenceTable = 
    ADDCOLUMNS(
        CommonAssets,
        "Difference",
        VAR Portfolio1Percent = LOOKUPVALUE([Portfolio1Percentage], Portfolio1Aggregated[AssetName], [AssetName])
        VAR Portfolio2Percent = LOOKUPVALUE([Portfolio2Percentage], Portfolio2Aggregated[AssetName], [AssetName])
        RETURN Portfolio1Percent - Portfolio2Percent
    )

VAR AverageDifference = 
    AVERAGEX(DifferenceTable, [Difference])

RETURN
    IF(
        SelectedCount = 2,
        AverageDifference,
        BLANK()
    )

 

 

Best Regards

hackcrr

If I have answered your question, please mark my reply as solution and kudos to this post, thank you!

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors