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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
nikhil425
Helper I
Helper I

How to apply ORDER BY inside UNION

Hello DAX gurus, 


Can you please help!

I am trying to order the data from one of the tables inside UNION. Below is my DAX query

 EVALUATE
VAR UNIONTable =
    UNION (
        SELECTCOLUMNS (
            CALCULATETABLE (
                'Funds_PI - Operations',
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[Operation Date] >= DATEVALUE ( "4/6/2016" )
                ),
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[Operation Date] <= DATEVALUE ( "5/31/2022" )
                ),
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[Operation Type] <> "Valuation"
                        && 'Funds_PI - Operations'[Operation Type] <> "Cash-Adjusted Valuation"
                        && 'Funds_PI - Operations'[Operation Type] <> "Commitment"
                ),
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[fundkey] = 312
                )
            ),
            "fundkey", 'Funds_PI - Operations'[fundkey],
            "OperationDate", 'Funds_PI - Operations'[Operation Date],
            "SignedCash", 'Funds_PI - Operations'[IRR Cashflow (USD)],
            "OperationType", 'Funds_PI - Operations'[Operation Type]
        ),
        /*Getting the end valuation from the cashflows*/
        SELECTCOLUMNS (
            CALCULATETABLE (
                'Funds_PI - Operations',
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[Operation Type] = "Valuation"
                        || 'Funds_PI - Operations'[Operation Type] = "Cash-Adjusted Valuation"
                ),
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[Operation Date] = DATEVALUE ( "5/31/2022" )
                ),
                FILTER (
                    ALL ( 'Funds_PI - Operations' ),
                    'Funds_PI - Operations'[fundkey] = 312
                )
            ),
            "fundkey", 'Funds_PI - Operations'[fundkey],
            "OperationDate", 'Funds_PI - Operations'[Operation Date],
            "SignedCash", 'Funds_PI - Operations'[IRR Cashflow (USD)],
            "OperationType", 'Funds_PI - Operations'[Operation Type]
        )
    ) --ORDER BY [OperationDate]
RETURN
    XIRR ( UNIONTABLE, [SignedCash], [OperationDate], 0.8 )

Below is the actual output from the Union Table, I can apply ORDER BY for uniontable, however when using the XIRR it doesn't allow me to ORDERBY. 

FundkeyOperationDateSignedCashOperationType
31211/22/2021 12:00:00 AM174509804Distribution
3126/8/2018 12:00:00 AM24377447Distribution
3129/5/2018 12:00:00 AM0Distribution
3129/5/2018 12:00:00 AM52177305Distribution
31211/30/2018 12:00:00 AM23641956Distribution
3123/27/2019 12:00:00 AM12428976Distribution
3126/18/2019 12:00:00 AM14049019Distribution
3129/10/2019 12:00:00 AM19100403Distribution
31211/27/2019 12:00:00 AM11123037Distribution
3123/23/2020 12:00:00 AM36219023Distribution
3126/4/2020 12:00:00 AM37247067Distribution
31212/10/2020 12:00:00 AM36788955Distribution
3129/30/2021 12:00:00 AM66666667Distribution
3128/31/2018 12:00:00 AM0Call
31211/30/2019 12:00:00 AM0Call
3124/6/2016 12:00:00 AM-2941176Call
3124/19/2016 12:00:00 AM-4411765Call
3125/2/2016 12:00:00 AM-3431373Call
3126/21/2016 12:00:00 AM3431373Call
3124/12/2017 12:00:00 AM-15196076Call
3125/2/2017 12:00:00 AM-50882353Call
31210/5/2017 12:00:00 AM-23529412Call
31211/16/2017 12:00:00 AM-103039218Call
3126/8/2018 12:00:00 AM-24377447Call
3129/5/2018 12:00:00 AM-50216520Call
31211/30/2018 12:00:00 AM-23641956Call
3123/27/2019 12:00:00 AM-12428976Call
3126/18/2019 12:00:00 AM-14049019Call
3129/10/2019 12:00:00 AM-19100403Call
31211/27/2019 12:00:00 AM-11123037Call
3123/23/2020 12:00:00 AM-30728828Call
3126/4/2020 12:00:00 AM-19517477Call
3125/31/2022 12:00:00 AM-3254186Cash-Adjusted Valuation



I want to make the cashflows in Ascending order, but the XIRR function is not liking the ORDER BY.

any help greatly appreciated, Thank you

1 ACCEPTED SOLUTION

Thanks for clarifying the issue @nikhil425 

 

For the purpose of XIRR, the "ordering" of cashflows is solely determined by the date expression provided to XIRR, in your example [OperationDate].

 

The "first cashflow" will be the cashflow corresponding to the minimum value of [OperationDate].

 

If you need to control the content of UNIONTable, you could filter or apply other operations to ensure that the cashflow on the earliest date meets certain criteria, or to control what the earliest date is.

 

ORDER BY can't be used in this situation (i.e. with a table variable).

Apart from DAX queries (EVALUATE ... ORDER BY ... ) there is no way to control the ordering of rows of a table within DAX.

 

Regards,

Owen

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

3 REPLIES 3
OwenAuger
Super User
Super User

Hi @nikhil425 

ORDER BY can only be used follwing the final table expression returned by an EVALUATE statement

i.e. EVALUATE <table> ORDER BY...

 

Given that your expression returns the scalar result of XIRR, presumably it is intended to be a measure - is that correct? It's not possible to use ORDER BY within a measure definition.

 

In any case, there is no ability (or need) to enforce the ordering of rows for a table provided as the first argument of XIRR.

 

XIRR can handle multiple occurrences of dates, though grouping the table by date before passing to XIRR may improve performance. XIRR will also treat the minimum date expression evaluated by the 3rd argument as the "first" date for the purpose of the calculation.

 

I note that calling XIRR using your particular sample table produces a result of 10.7% if a guess argument <= 0.57 is provided.

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Thank you  @OwenAuger for your reply. 

The reason I wanted to order the cashflows is because sometime the order show 0 cashflow as the very first cashflow and XIRR doesn't calculate if the first cashflow is 0. But if I order them ascending then my first cashflow will always have a number. 

In my example above, I can apply the ORDER BY in the Var UNION Table above, which I commented out. 

I get an error when I use the Temptable in XIRR function that has an ORDER BY. I am assuming may there are some other ways to create a table with pre-order setup. 





Thanks for clarifying the issue @nikhil425 

 

For the purpose of XIRR, the "ordering" of cashflows is solely determined by the date expression provided to XIRR, in your example [OperationDate].

 

The "first cashflow" will be the cashflow corresponding to the minimum value of [OperationDate].

 

If you need to control the content of UNIONTable, you could filter or apply other operations to ensure that the cashflow on the earliest date meets certain criteria, or to control what the earliest date is.

 

ORDER BY can't be used in this situation (i.e. with a table variable).

Apart from DAX queries (EVALUATE ... ORDER BY ... ) there is no way to control the ordering of rows of a table within DAX.

 

Regards,

Owen

 

 


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors