March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hello, Trying to calculate XIRR, but the result varies even on the sort order of values.
My DAX to calculate a table, and use it to calculate XIRR is as follows:
Output is :
I wish to order the table in a manner that the output is sorted where for a given date the negative(-) values come first.
Desired output:
Please help, how i can modify my code? Thank you
Solved! Go to Solution.
I managed to sort the dynamic table within my measure. I needed to create a caculated column which has 0 if the sum of Net is zero for a given date for any deal name. Create another column to be 1 if value in this new column is <0 else 2.
Now split into 3 parts and then combine using union function.
My measure:
Hello, My DAX to calculate a table is as follows:
8/7/2024 12:00:00 AM | 97643520 |
8/7/2024 12:00:00 AM | -1017120 |
8/7/2024 12:00:00 AM | -96626400 |
8/8/2024 12:00:00 AM | -97643520 |
9/30/2024 12:00:00 AM | 103015680 |
I wish to order the table in a manner that the output is sorted where for a given date the negative(-) values come first.
Desired output:
GL Date | Net |
8/7/2024 | -96626400 |
8/7/2024 | -1017120 |
8/7/2024 | 97643520 |
8/8/2024 | -97643520 |
9/30/2024 | 103015680 |
Any idea, how i can modify my code? TIA
Hi @visheshvats1 - DAX doesn’t have a direct way to sort a calculated table within the table expression itself. You can add a sorting column to your DAX code to specify the order of rows within the table.
Use below code:
VAR __Table =
UNION(
SELECTCOLUMNS(
FILTER(
'General Ledger',
'General Ledger'[GL Date] >= [MIN Date] &&
'General Ledger'[GL Date] <= [MAX Date] &&
'General Ledger'[Trans Type] IN Trantype
),
"GL Date", 'General Ledger'[GL Date],
"Net", 'General Ledger'[Credits only] - 'General Ledger'[Debits only]
),
ROW(
"GL Date", [MAX Date],
"Net", [Net sheet 1]
)
)
VAR __SortedTable =
ADDCOLUMNS(
__Table,
"SortOrder",
RANKX(
__Table,
[GL Date] & IF([Net] < 0, "0", "1") & ABS([Net]),
,
ASC
)
)
RETURN
SELECTCOLUMNS(
TOPN(
COUNTROWS(__SortedTable),
__SortedTable,
[GL Date], ASC,
[SortOrder], ASC
),
"GL Date", [GL Date],
"Net", [Net]
)
Try the above code, you can get negative values first and remaining as expected in sort order.
Proud to be a Super User! | |
You can achieve this by creating a calculated column that assigns a sort order based on the sign of the values. Here’s an example of how you can modify your DAX code:
Create a calculated column to determine the sort order:
SortOrder = IF([Value] < 0, 0, 1)
Sort your table by the date and the new SortOrder column:
SortedTable =
ADDCOLUMNS(
YourTable,
"SortOrder", IF([Value] < 0, 0, 1)
)
Use the SortedTable to calculate XIRR:
XIRRResult =
XIRR(
ADDCOLUMNS(
YourTable,
"SortOrder", IF([Value] < 0, 0, 1)
),
[Value],
[Date]
)
This approach ensures that for each date, the negative values will appear first, followed by the positive values.
Best Regards
Saud Ansari
If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!
Apologies, for my lapse in understanding. The table I refferd to earlier is not a physical table.
My XIRR calculation Measure is :
As the mathematical function 'XIRR' needs the order of the values in an order where neagative values come before positive values at a given date. I wish to order the output of my temporary table within my measure.
Hope my question is clearer now.
@lbendlin @Ritaf1983
As the mathematical function 'XIRR' needs the order of the values in an order where neagative values come before positive values at a given date.
That's not what the function definition says. It does however require a grouping by date.
you would still need to provide more realistic sample data.
Thanks for the response, as XIRR measures the annualized return on an investment portfolio, in accounting scenarios, the initial investment is always denoted as a -ve value, this can be checked on an excel sheet using the tabular data I shared.
Another solution to this problem can be that if we could modify the code such that if the sum of [Net] for a given [GL Date] is 0 then those rows be excluded from the calculation altogether. Can you please suggest a way to achieve that?
Good solution, but some doesnt work. The change FIlter/Sort needs to be applied at the table level.
While trying something,
Any idea why the Table is not being sorted by the [Net] values?
Output is still:
There is no need for sorting the raw data.
Understood, and yes I could get the value using the expression share by you earlier. Thank you for that.
But this only works if i filter the table with a given 'deal name'. Not for each deal.
Working Table dax:
But when i modify to accept any 'deal name':
The subtotal is being repeated over and over:
Any idea on how to resolve this? Thank you.
Please provide sample data that fully covers your issue. In a usable form (not as screenshots).
Please show the expected outcome based on the sample data you provided.
Sorry, I've been trying to upload the data file, but somehow I can't see any upload button when i click reply. Any idea how i can best share the data file here?
Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Min date = min(GL Date)
Max date = 9/30/2024
Trans Type filters:
Trans Types | |
For Cash Flows(Net): credits only - debits only | For NAV (Net Sheet 1): debits only - credits only |
Accrued portfolio dividend receivable, | Accumulated Depreciation, |
Accrued Rental Income, | Acquisition Adjustment, |
Bond - interest receivable, | Adjustment- do not post, |
Capitalized fee/expense, | Adjustment- do not post (Recallable), |
Distribution - Current income - (Recallable), | Bond - interest receivable, |
Distribution - Current income (Non-Recallable), | Bonus Shares, |
Dividend Income - Recallable, | Capitalized fee/expense, |
Gain: realized f/x, long term - FOF, | Distribution - Current income - (Recallable), |
Gain: realized, long term - FOF, | Distribution - Current income (Non-Recallable), |
Income: dividend - cash, | Dividend Income - Recallable, |
Income: dividend - pik, | Investment Property, |
Income: dividend accrual, | Loan - Interest Receivable, |
Income: fees, investment, | Loan Receivable, |
Income: interest - cash, investment, | Purchase, |
Income: interest - pik, | Purchase - FOF - Call for Fund Expenses (aff Cmt), |
Income: interest accrual, | Purchase - FOF - Call for Fund Expenses (Not aff Cmt), |
Income: other, investment, | Purchase - FOF - Call for Investments, |
Investment Property, | Purchase - FOF - Call for Management Fees (aff Cmt), |
Investments Payable, | Purchase - FOF - Call for Management Fees (Not aff Cmt), |
Investments Receivable, | Purchase - FOF - Call for Operational Expenses (aff Cmt), |
Loan - Interest Receivable, | Purchase - FOF - Call for Operational Expenses (Not aff Cmt), |
Loan Receivable, | Purchase: PIK, |
Loss: realized f/x, long term, | Purchase: security conversion, |
Loss: realized f/x, long term, | Realized Gains/Losses - FOF (Non Recallable), |
Loss: realized, long term, | Realized Gains/Losses - FOF (Recallable), |
Other receivable, | Receivable: interest, |
Purchase, | Receivable: interest, accrued, |
Purchase - FOF - Call for Fund Expenses (aff Cmt), | Return of capital, |
Purchase - FOF - Call for Fund Expenses (Not aff Cmt), | Return of Capital - FOF (Non Recallable), |
Purchase - FOF - Call for Investments, | Return of Capital - FOF (Recallable), |
Purchase - FOF - Call for Management Fees (aff Cmt), | Return of capital: stock distribution to partners, |
Purchase - FOF - Call for Management Fees (Not aff Cmt), | Return of Excess Contributions, |
Purchase - FOF - Call for Operational Expenses (aff Cmt), | Sell, |
Purchase - FOF - Call for Operational Expenses (Not aff Cmt), | Sell- do not use, |
Purchase: PIK, | Unrealized appreciation/depreciation, |
Purchase: security conversion, | Unrealized depreciation, |
Realized Gains/Losses - FOF (Non Recallable), | Unrealized f/x appreciation/depreciation |
Realized Gains/Losses - FOF (Recallable), | |
Receivable: interest, | |
Receivable: interest, accrued, | |
Rental income, | |
Return of capital, | |
Return of Capital - FOF (Non Recallable), | |
Return of Capital - FOF (Recallable), | |
Return of capital: stock distribution to partners, | |
Return of Excess Contributions, | |
Sell, | |
Withholding tax on Dividend |
Data:
|
I managed to sort the dynamic table within my measure. I needed to create a caculated column which has 0 if the sum of Net is zero for a given date for any deal name. Create another column to be 1 if value in this new column is <0 else 2.
Now split into 3 parts and then combine using union function.
My measure:
Hi,@visheshvats1
We are delighted that you have found a solution and are willing to share it.
Accepting your post as the solution is incredibly helpful to our community, as it enables members with similar issues to find answers more quickly.
Thank you for your valuable contribution to the community, and we wish you all the best in your work.
Best Regards,
Leroy Lu
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
133 | |
90 | |
88 | |
64 | |
58 |
User | Count |
---|---|
202 | |
137 | |
106 | |
70 | |
68 |