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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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