DAX Measure IF statement with Variables - if-statement

I have a dax measure that adds a thousands separator to my card totals in Power BI, converting them to a string (I am also including a currency symbol in another measure to the same card which is why it needs to be a string).
I want to add an if statement that will say if the total amount is < 1000, then remove the thousands separator from the total. Currently numbers like £500 appear with the separator before the total e.g.
,£500. Here is the code:
Card Total =
VAR right =
RIGHT ( [Total Amount], 3 )
VAR left =
SUBSTITUTE ( [Total Amount] , right, "" )
RETURN
COMBINEVALUES ( ",", left, right )

The following measure will not include a comma when the total is under 1000. I don't know how large the values you're working with are, but if they exceed 6 digits you'll also want to add additional commas where necessary.
Card Total =
VAR RIGHT =
RIGHT ( [Total Amount], 3 )
VAR LEFT =
SUBSTITUTE ( [Total Amount] , RIGHT, "" )
RETURN
IF (
[Total Amount] < 1000
RIGHT,
COMBINEVALUES ( ",", LEFT, RIGHT )
)

Related

Power BI, Line Chart with Base Value 100

I need to build a line chart that is supposed to show the cumulative increase from Day 1 to the End.
An Example would be like this:
Date Capital Value
31-Jan 237 100.00
28-Feb 250 105.48
31-Mar 210 88.60
30-Apr 300 126.58
In other words, is dividing every value by the first value, not of the whole table but the dates that are been displayed in the chart. The first date is dynamically changed as time passes, making it impossible to fix the formula to 31-Jan for example
Kind Regards
Try this measure:
% Capital running total in Date =
VAR runtotal =
CALCULATE (
SUM ( 'YourTable'[Capital] ),
FILTER (
ALLSELECTED ( 'YourTable'[Date] ),
ISONORAFTER ( 'YourTable'[Date], MAX ( 'YourTable'[Date] ), DESC )
)
)
VAR baseval =
CALCULATE (
SUM ( 'YourTable'[Capital] ),
FIRSTDATE ( ALL ( YourTable[Date] ) )
)
RETURN
DIVIDE ( runtotal, baseval )
Note that for calculating the Running Total Power BI can help you with a Quick Measure.

Sum 5 lines of the max values in a column and get the % in power bi

I have a table with some contracts, like this:
Contract Num
Another header
Value
123456
Cleaning
123.541,00--
544311
Security
200.000,00--
254856
Cleaning
23.581,00--
048941
Security
80.000,00--
128546
Cleaning
300.500,00--
255311
Security
99.000,00--
130056
Cleaning
543.541,00--
859311
Security
10.000,00--
I want to get this in Power Bi:
The value of the 5 most expensive contracts
How much these 5 represents in % of the total sum.
I got the letter A in a card applying filters (N Superior), but I can't work with it in a measure. What expression can I use to get a number of line in a column? How can I solve this in a measure?
thanks
Hello Please test this DAX Code:
This gives you the TOP5 Total:
Top5_Total =
VAR TOP5Total =
SUMX ( TOPN ( 5, YourTable, [Value] ), [Value] )
RETURN
TOP5Total
And This gives you the ratio of Top5 To All Totals:
RatioOfTop5_To_Total =
VAR TOP5Total =
SUMX ( TOPN ( 5, YourTable, [Value] ), [Value] )
VAR Total =
CALCULATE ( SUM ( YourTable[Value] ), ALL ( YourTable ) )
VAR PercentOfTop5_To_Total =
DIVIDE ( 100 * TOP5Total, Total )
RETURN
PercentOfTop5_To_Total

SUMMARIZE or SUM values based on Date fields in another table

I am trying to do a burndown graph on PowerBI.
My data is a list of tasks. Each task has a numerical value (EFFORT) assigned to it and there is a total amount of effort for any given month (sum of all EFFORT). As tasks as set to DONE, the ongoing effort should be deducted from a running total and that value used to plot a graph. I have 3 columns
I would like to have measure to calculate EFFORT REMAINING for each date, i.e.
EFFORT REMAINING = TOTAL EFFORT - (EFFORT WHEN TASKS ARE DONE FOR EACH DAY)
For example,
I did get the consecutive dates displaying:
Burndown = CALENDAR(DATE(2022,7,1),DATE(2022,7,31))
and also the total effort (starting value)
TOTAL EFFORT = SUM(Issues[EFFORT])
Now for each date in table, I need to minus the accumulating total of EFFORT when the status is set to DONE
EFFORT REMAINING = Burndown[TOTAL EFFORT]-SUM(Issues[EFFORT]="DONE" ....
Im stuck after this last point. Can anyone help, please?
you are so close to the answer ). Convert SUM(Issues[EFFORT]="DONE" to:
CALCULATE(
SUM(Issues[EFFORT])
, SUM(Issues[Status]="DONE"
)
Have a nice day.
Please try this measure:
Please ensure that (1-Many) relationship is created between Burndown [Date] and Issues[ISSUE_CREATED] columns.
EFFORT REMAINING =
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( Issues, Burndown[Date] ),
"Total Effort", CALCULATE ( SUM ( Issues[EFFORT] ) ),
"Tasks Completed", CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" ),
"Effort Remaining",
CALCULATE ( SUM ( Issues[EFFORT] ) )
- CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" )
)
VAR Result =
SUMX ( TblSummary, [Effort Remaining] )
RETURN
Result
After that, you can create a clustered column chart, and put [Date field] on calendar table on X_axis and put 'EFFORT REMAINING' measure on Y_axis(Value axis) to see the result.
I hope It solves your problem.
Bonus Info:
If you want to see your Summary table, create a "New Table" and paste this code:
Summary_Table =
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( Issues, Burndown[Date] ),
"Total Effort", CALCULATE ( SUM ( Issues[EFFORT] ) ),
"Tasks Completed", CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" ),
"Effort Remaining",
CALCULATE ( SUM ( Issues[EFFORT] ) )
- CALCULATE ( SUM ( Issues[EFFORT] ), Issues[STATUS] = "DONE" )
)
VAR Result =
SUMX ( TblSummary, [Effort Remaining] )
RETURN
TblSummary
The result It produces:
Note: I have limited access to your data sets as you shared above. The result will be exact with your full dataset.

DAX using IF and AND functions

I'm trying to create a trending table with a value and a forecast. The forecast needs to start from the current month going forward. I am using this dax function:
Spend Forecast =
IF (
OR (
DIVIDE (
CALCULATE (
SUM ( refv_Spend_Cap[Spend_2019] ),
FILTER ( refv_Spend_Cap, refv_Spend_Cap[Split] = "Spend Actual" )
),
1000000
)
< 1,
DIVIDE (
CALCULATE (
SUM ( refv_Spend_Cap[Spend_2019] ),
FILTER ( refv_Spend_Cap, refv_Spend_Cap[Ind_Monthend] = "x" )
),
1000000
)
< 1
),
DIVIDE (
CALCULATE (
SUM ( refv_Spend_Cap[Spend_2019] ),
FILTER ( refv_Spend_Cap, refv_Spend_Cap[Split] = "Spend Forecast" )
),
1000000
),
""
)
The formula is calculating to check if these two conditions are met:
if there's no value then populate the forecast or if the ind monthend = 'x' then it should populate, if those two conditions are not met then it should leave it blank.
There are no syntax errors on the query but i am getting this error:
The True/False expression does not specify a column. Each True/False
expressions used as a table filter expression must refer to exactly
one column
Where did I go wrong?
It's very hard to comprehend such long formulas. It's the best practice to break them down into multiple measures. For example, your code can be re-writen as follows:
Create base measure:
Total Spend = SUM ( refv_Spend_Cap[Spend_2019] ) / 1000000
Now re-use the base measure to create 3 conditional measures. No need to use FILTER here:
Spend Actual = CALCULATE ( [Total Spend], refv_Spend_Cap[Split] = "Spend Actual" )
Spend X = CALCULATE ( [Total Spend], refv_Spend_Cap[Ind_Monthend] = "x" )
Spend Forecast = CALCULATE ( [Total Spend], refv_Spend_Cap[Split] = "Spend Forecast" )
Then the final result is simply:
Forecast = IF ( [Spend Actual] < 1 || [Spend X] < 1, [Spend Forecast], "")
It's much easire to understand what's happening, and easier to debug. You will also gain perfomance bonus because (usually) re-used measures are cached and calculated only once.
Try this code, if it still gives you problems, describe the new error and I'll help you fix it.
BTW, there is a popular free tool to format your DAX code:
Dax Formatter

DAX custom Grand Total not behaving as expected

I have a Measure which calculates a cumulative total:
CumulativeCount:=
VAR date1 = MAX( DimDate[Date] )
VAR date2 = MAX( FactTable[EndDate] )
RETURN
CALCULATE (
SUM( FactTable[Count] ),
DimDate[Date] <= date1,
DimDate[Date] <= date2,
ALL( DimDate[Date] )
)
And another, actually used in the Pivot Table, which, when it's calculating the Grand Total, is supposed to add up the cumulative totals for each date:
CumulativeCountForPivot:=
IF (
-- If calculating for one group
COUNTROWS( VALUES( FactTable[Group] ) ) = 1,
-- Do core logic
[CumulativeCount],
-- Else add up the results from each group
SUMX(
VALUES( FactTable[Group] ),
[CumulativeCount]
)
)
I don't understand why the Grand Total in the final column is 12, not 6.
The reason is that the grand total is for GroupA and GroupB combined and there is a cumulative count of 12 on that date (6 for each group).
On 06/01/2017 there are no records for GroupA so the [CumulativeCount] measure is returning a blank, even though there are records before that date and the count would be 6. If you added a record for GroupA with a Count of 0 on 06/01/2017, then you would see 6 appear.
If you want a measure that only shows 6 on that date, then try something like this:
CountForPivot =
VAR TempTable = SUMMARIZE(FactTable,
FactTable[Group],
FactTable[EndDate],
"Cumulative",
CALCULATE(SUM(FactTable[Count]),
FILTER(ALLEXCEPT(FactTable, FactTable[Group]),
FactTable[EndDate] <= MAX(FactTable[EndDate])
)
)
)
RETURN SUMX(TempTable, [Cumulative])