Forum Discussion
DAX Calculation
- 7 years ago
Hi esilva32
It certainly looks like it would be easier to do this in the query editor. If you do need to do it in DAX, try this. Create a new calculated table:
TableB = VAR _BaseTable = ADDCOLUMNS ( GENERATE ( DISTINCT ( TableA[Id] ); GENERATESERIES ( CALCULATE ( DISTINCT ( TableA[StartDate] ) ); CALCULATE ( DISTINCT ( TableA[EndDate] ) ) ) ); "TempVal"; CALCULATE ( DISTINCT ( TableA[Value] ) ) ) VAR _Dates = DISTINCT ( SELECTCOLUMNS ( _BaseTable; "Date"; [Value] ) ) VAR _ResTable = ADDCOLUMNS ( _Dates; "Total Value"; SUMX ( _BaseTable; IF ( [Value] = [Date]; [TempVal] ) ) ) RETURN _ResTable - 7 years ago
Hello guys
Thank you for your help. @AIB thank you, worked perfectly, thanks for the help.
Regards, Portugal.
- 7 years ago
Minor modification:
TableB = VAR _BaseTable = ADDCOLUMNS ( GENERATE ( DISTINCT ( TableA[Id] ); GENERATESERIES ( CALCULATE ( DISTINCT ( TableA[StartDate] ) ); MIN ( TODAY (); CALCULATE ( DISTINCT ( TableA[EndDate] ) ) ) ) ); "TempVal"; CALCULATE ( DISTINCT ( TableA[Value] ) ) ) VAR _Dates = DISTINCT ( SELECTCOLUMNS ( _BaseTable; "Date"; [Value] ) ) VAR _ResTable = ADDCOLUMNS ( _Dates; "Total Value"; SUMX ( _BaseTable; IF ( [Value] = [Date]; [TempVal] ) ) ) RETURN _ResTable
You can expand out your rows in Power Query using something like:
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Dates( [Initial Date], Number.From( [Final Date]- [Initial Date]) +1, #duration(1,0,0,0))),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom")
There is also GENERATESERIES in DAX. But, depending on what you want to do, check out these two Quick Measures as I think you want something like them.
https://community.powerbi.com/t5/Quick-Measures-Gallery/Open-Tickets/m-p/409364
https://community.powerbi.com/t5/Quick-Measures-Gallery/Periodic-Billing/m-p/409365
- AlB7 years agoCommunity Champion
Hi esilva32
It certainly looks like it would be easier to do this in the query editor. If you do need to do it in DAX, try this. Create a new calculated table:
TableB = VAR _BaseTable = ADDCOLUMNS ( GENERATE ( DISTINCT ( TableA[Id] ); GENERATESERIES ( CALCULATE ( DISTINCT ( TableA[StartDate] ) ); CALCULATE ( DISTINCT ( TableA[EndDate] ) ) ) ); "TempVal"; CALCULATE ( DISTINCT ( TableA[Value] ) ) ) VAR _Dates = DISTINCT ( SELECTCOLUMNS ( _BaseTable; "Date"; [Value] ) ) VAR _ResTable = ADDCOLUMNS ( _Dates; "Total Value"; SUMX ( _BaseTable; IF ( [Value] = [Date]; [TempVal] ) ) ) RETURN _ResTable- esilva327 years agoFrequent Visitor
Hello guys
Thank you for your help. @AIB I tried your code and the message was this: it is not possible to convert multiple columns to a scalar value.
Guys, in Power Query I have to build a new table to put this code in it?
- AlB7 years agoCommunity Champion
It works fine on my end. Check it out in this file. Maybe you've done something differently.
On a different note, please always show your sample data in text-tabular format in addition to (or instead of) the screen captures. That allows people trying to help to readily copy the data and run a quick test, plus it increases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here. Or, ideally, share the pbix (beware of confidential data).