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
AlanRGroskreutz
Helper II
Helper II

SUMX using ALLSELECTED with virtual tables to calculate an unfiltered constant

Hi there,

I've run into a problem that I can't resolve, not even using the Bing AI chat (which is actually really good).

I am trying to set up a what-if simulator that will see how changes in flights out of a group of airports in a region will be affected if one of them increases their flights. The relation will be related to the distance between the airport that has the change and the other airports. I am trying to create a DAX measure that will redistribute the sum of the flights of the group by increasing the flights at the selected airport by the change parameter and then reduce the flights from the other airports depending upon their distance from the selected airport.  It's a sort of market share "what-if", where the market size doesn't change. and the new costomers that go to the selected location are taken from the other nearby locations. 

Here is the DAX measure code:

 

New Flights based on distance = 
//Airport slicer
VAR sa =
    SELECTEDVALUE ( 'DepartureSlicer2'[AirportCode] ) //total flights in selection
VAR fl =
    SUMX ( ALLSELECTED ( 'Flight details' ), 'Flight details'[Flights] ) // original value for selected airport
VAR o =
    CALCULATE (
        SUM ( 'Flight details'[Flights] ),
        'Flight details'[adep] = sa
    ) // change for selected airport
VAR c = ( 1 + [Change Value] ) * o // compensatory change for all other airports


//Airport Location Variables
VAR Lat1 =
    CALCULATE (
        MIN ( 'Departure Airports'[Latitude] ),
        FILTER (
            'Flight details',
            'Flight details'[adep]
                = SELECTEDVALUE ( 'DepartureSlicer2'[AirportCode] )
        )
    )
VAR Lng1 =
    CALCULATE (
        MIN ( 'Departure Airports'[Longitude] ),
        FILTER (
            'Departure Airports',
            'Departure Airports'[AirportCode]
                = SELECTEDVALUE ( 'DepartureSlicer2'[AirportCode] )
        )
    )
VAR Lat2 =
    CALCULATE (
        MIN ( 'Departure Airports'[Latitude] ),
        FILTER (
            'Departure Airports',
            'Departure Airports'[AirportCode] = FIRSTNONBLANK ( 'Flight details'[adep], 1 )
        )
    )
VAR Lng2 =
    CALCULATE (
        MIN ( 'Departure Airports'[Longitude] ),
        FILTER (
            'Departure Airports',
            'Departure Airports'[AirportCode] = FIRSTNONBLANK ( 'Flight details'[adep], 1 )
        )
    ) //Distance Algorithm Variables 
VAR P =
    DIVIDE ( PI (), 180 )
VAR A =
    0.5
        - COS ( ( Lat2 - Lat1 ) * p ) / 2
        + COS ( Lat1 * p )
            * COS ( lat2 * P )
            * (
                1
                    - COS ( ( Lng2 - Lng1 ) * p )
            ) / 2
VAR final =
    12742 * ASIN ( ( SQRT ( A ) ) )
// Virtual table of distances
VAR FirstTable =
    SUMMARIZE (
        'Flight details',
        'Flight details'[adep],
        "orig", SUM ( 'Flight details'[Flights] ),
        "dist", 12742 * ASIN ( ( SQRT ( A ) ) )
    ) 
//final Calculation
VAR Flights =
    SELECTCOLUMNS ( FirstTable, "orig", [orig] )
VAR Distance =
    SELECTCOLUMNS ( FirstTable, "Dist", [dist] )
VAR InverseDistance =
    IF ( Distance = 0, BLANK (), 1 / Distance )

----This is where the error occurs.  Code before this has been checked and works------
VAR TotalInverseDistance =
    SUMX ( allselected(FirstTable), IF ( [dist] = 0, BLANK (), 1 / Distance ) )

VAR SecondTable =
	ADDCOLUMNS(FirstTable,"NewFlights",
	IF ( Distance = 0, 0, Flights * ( InverseDistance / TotalInverseDistance ) )
	)
	
RETURN
    SUMX(SecondTable,[NewFlights])

 

The measure is almost working.  I have placed a comment where I'm getting the error 
(SUMX ( allselected(FirstTable), IF ( [dist] = 0, BLANK (), 1 / Distance ) ))

 I get the error that the ALLSELECTED function is being given a table expression and not a table reference.

How can I calculate this constant value (not filtered by Flight details'[adep]) from a virtual table?

Here are some test tables
Departure Airports

AirportCodeLatitudeLongitudeCityCountry
EGGW5.187.469.864-0.368333012LondonUnited Kingdom
EGKK51.148.102-0.190278LondonUnited Kingdom
EGLC51.505.2990.055278LondonUnited Kingdom
EGLL514.706-0.461941LondonUnited Kingdom
EGLW5.146.972.275-0.179444LondonUnited Kingdom
EGSS5.188.499.8320.234999999LondonUnited Kingdom

 

Flight details

adepFlights
EGGW137
EGKK2189
EGLC14
EGLL3789
EGSS1177



Here is the Relationship diagram 

AlanRGroskreutz_1-1685021289137.png

 

I think that's all the data one might need to understand what's going on.  Any help will be appreciated.

3 REPLIES 3
AlanRGroskreutz
Helper II
Helper II

Good question,  let me try to walk through how I got there.
First of all, the formula I'm using to calculate the new flights for each airport (except the one where distance is =0) is: 

AlanRGroskreutz_0-1685358395052.png

where
Fs = the sum of the flights from the non-zero distance airports,
d = distance from an airport in the group to the selected airport

In this context, the line 

 

SUMX ( allselected(FirstTable), IF ( [dist] = 0, BLANK (), 1 / Distance ) )

 

is trying to calculate the sumation in the denominator of the above formula. 

If I just leave it as a SUMX, without the ALLSELECTED included, it doesn't seem to retain the sumation; it just gets filtered by the 'Flight details'[adep] values.  It works at the beginning of the code to calculate the variable fl 

 

VAR fl =
    SUMX ( ALLSELECTED ( 'Flight details' ), 'Flight details'[Flights] )

 

but that is referencing an datatable in the model, not a virtual table.

Here is the end of the code again (a bit more cleaned up) and a results table showing the different variables.

 

VAR FirstTable =
    SUMMARIZE (
        'Flight details',
        'Flight details'[adep],
        "orig", SUM ( 'Flight details'[Flights] ),
        "dist", 12742 * ASIN ( ( SQRT ( A ) ) )
    ) //final Calculation
VAR Flights =
    SELECTCOLUMNS ( FirstTable, "orig", [orig] ) 
VAR Distance =
    SELECTCOLUMNS ( FirstTable, "Dist", [dist] )
VAR InverseDistance =
    IF ( Distance = 0, BLANK (), 1 / Distance )
VAR TotalInverseDistance =
    SUMX ( FirstTable, IF ( [dist] = 0, BLANK (), 1 / Distance ) )
//VAR SecondTable =
//	ADDCOLUMNS(FirstTable,"NewFlights",
//	IF ( Distance = 0, 0, Flights * ( InverseDistance / TotalInverseDistance ) )
//	)
RETURN
TotalInverseDistance

 

adepFlightsKilometersVARIABLE-flVARIABLE-DistanceVARIABLE-InverseDistanceVARIABLE-TotalInverseDistance
EGGW87050,47690500,0198430,019843
EGKK328943,237690430,0231330,023133
EGLC140076900  
EGLL201736,027690360,0277630,027763
EGSS1374447690440,0227270,022727


So to sum up, the context of the ALLSELECTED is an attempt to do in the TotalInverseDistance variable with the distances what the variable fl did with the flights.  This will be used later on in the code to divide up the flights based upon their distance from the selected airport.

SUMX(FILTER(FirstTable,[dist]>0),divide(1,[dist]))

 

If in doubt use EVALUATEANDLOG to inspect the results of the intermediate steps.

lbendlin
Super User
Super User

I get the error that the ALLSELECTED function is being given a table expression and not a table reference.

What made you try to use ALLSELECTED in this context?

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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