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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Convert DAX Measure to SQL

Hi Experts

 

What is the SQL equivalent of the following DAX (Power BI) Formula
AOV = CALCULATE(SUM(FACTSalesOrderTable[Gross_Order_Value]),ALLEXCEPT(FACTSalesOrderTable,FACTSalesOrderTable[increment_id]))

1 ACCEPTED SOLUTION
Anonymous
Not applicable

AOV =
CALCULATE(
    SUM( FACTSalesOrderTable[Gross_Order_Value] ),
    ALLEXCEPT(
        FACTSalesOrderTable,
        FACTSalesOrderTable[increment_id]
    )
)

// is equivalent to this SQL query:

SELECT
    sum( so[Gross_Order_Value] )
from dbo.FACTSalesOrderTables as so
where (
    so.increment_id in (       
//     1. if there is an active filter on increment_id
//        then you need all the values that the filter
//        uses. All other filters are removed from the
//        expanded fact table.
//     2. if there's no active filter on increment_id
//        this where clause must disappear from the
//        query completely.
//     This is the semantics of ALLEXCEPT.
    )
)

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

AOV =
CALCULATE(
    SUM( FACTSalesOrderTable[Gross_Order_Value] ),
    ALLEXCEPT(
        FACTSalesOrderTable,
        FACTSalesOrderTable[increment_id]
    )
)

// is equivalent to this SQL query:

SELECT
    sum( so[Gross_Order_Value] )
from dbo.FACTSalesOrderTables as so
where (
    so.increment_id in (       
//     1. if there is an active filter on increment_id
//        then you need all the values that the filter
//        uses. All other filters are removed from the
//        expanded fact table.
//     2. if there's no active filter on increment_id
//        this where clause must disappear from the
//        query completely.
//     This is the semantics of ALLEXCEPT.
    )
)
themistoklis
Community Champion
Community Champion

Hello @Anonymous 

 

You should use a window function.

 

Try something like the following:

 

SELECT dimension1

            , dimension2,

            SUM(Gross_Order_Value) OVER (PARTITION BY increment_id) AS AOV

FROM FACTSalesOrderTable

Anonymous
Not applicable

thanks thenistoklis and daxer

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors