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
IgorM
Frequent Visitor

Creating a temporary (VAR) table and filtering it

Hi,

I have a question about data modelling and/or DAX query.

Below can be found a simplified data model. This model is built in Excel (Power Pivot).

2022-12-22_23h00_54.png

There is a Transactions table that is a fact table holding data from an accounting system.

The are also two tables (Areas and ConversionFactors) that currently have no relationships to any other tables.

What I need to do is create a report showing for every SegmentID and a finance year a margin amount calculated as follows:

  • sum of the transactions for a given segment (i.e. sum of the amounts from the Value column from the Transactions table)

multiplied by

  • the segment’s area for that year (SegmentArea from table Areas) multiplied by the segment’s conversion factor for the year (ConversionFacto from table ConversionFactors)

divided by

  • the product of multiplying the areas of ALL the crops by their respective conversion factors for that year.

However I’m not sure how to calculate such an amount. I’ve tried using NATURALLEFTOUTERJOIN to create a temporary (VAR) table by joining Areas and ConversionFactors however there were two issues with that. Firstly, I was only able to join it by one column whereas here they need to be joined by Year and SegmentID. Secondly, I didn’t seem to be able to filter that temporary table to only get values for a current SegmentID.

Any ideas/suggestions will be much appreciated.

Kind regards,

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

I would consider merging Areas and ConversionFactors into Transactions, so you have SegmentArea and ConversionFactor as columns in that table.

 

Then you could write something like this (untested):

VAR _Summary_ =
    CALCULATETABLE (
        SUMMARIZE (
            Transactions,
            Transactions[SegmentID],
            Transactions[SegmentArea],
            Transactions[ConversionFactor],
            "@SumValue", SUM ( Transactions[Value] )
        ),
        ALL ( Segments )
    )
VAR _CurrSubtable_ =
    FILTER ( _Summary_, Transactions[SegmentID] IN VALUES ( Segments[SegmentID] ) )
RETURN
    DIVIDE (
        SUMX ( _CurrSubtable_, [@SumValue] * [SegmentArea] * [ConversionFactor] ),
        SUMX ( _Summary_,      [@SumValue] * [SegmentArea] * [ConversionFactor] )
    )

View solution in original post

1 REPLY 1
AlexisOlson
Super User
Super User

I would consider merging Areas and ConversionFactors into Transactions, so you have SegmentArea and ConversionFactor as columns in that table.

 

Then you could write something like this (untested):

VAR _Summary_ =
    CALCULATETABLE (
        SUMMARIZE (
            Transactions,
            Transactions[SegmentID],
            Transactions[SegmentArea],
            Transactions[ConversionFactor],
            "@SumValue", SUM ( Transactions[Value] )
        ),
        ALL ( Segments )
    )
VAR _CurrSubtable_ =
    FILTER ( _Summary_, Transactions[SegmentID] IN VALUES ( Segments[SegmentID] ) )
RETURN
    DIVIDE (
        SUMX ( _CurrSubtable_, [@SumValue] * [SegmentArea] * [ConversionFactor] ),
        SUMX ( _Summary_,      [@SumValue] * [SegmentArea] * [ConversionFactor] )
    )

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.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.