the sum function cannot work with string values - string

I'm working with DAX in PowerBI. I hava a column with 80 000 string values.
70% of these values is "European Desk". I want to show this percentage. It's string value, i don't understand how to do it with DAX
Any advice ?

The measure you are looking for is
% European Desk = DIVIDE(
CALCULATE(
COUNT('Table'[String]),
'Table'[String] = "European Desk"
),
COUNT('Table'[String])
)
With CALCULATE you can change the filter context for the COUNT() aggregation.
You can apply this formula to e.g. this table:
Table = DATATABLE(
"Index", INTEGER,
"String", STRING,
{
{1, "European Desk"},
{2, "European Desk"},
{3, "European Desk"},
{4, "African Desk"},
{5, "Asian Desk"}
}
)

Related

Excel Power Pivot - Nested IF OR Formula error

I have been trying to use the formula mentioned below which is not working and showing an error message stating "Power pivot expression that yield variant data-type cannot be used to define calculated columns"
IF (
OR ( Table2[TeamStatus] = "Lost", Table2[TeamStatus] = "Won't proceed" ),
"0",
IF (
OR ( Table2[TeamStatus] = "Next level", Table2[TeamStatus] = "Disqualified" ),
"0",
IF (
AND ( YEAR( Table2[Match Date] ) = YEAR( TODAY() ), [TeamStatus] <> "Won" ),
[Spent Amount],
0
)
)
)
Data types of the columns
1) [TeamStatus] = Text
2) [Match Date] = Date
3) [Spent amount] = Number
The purpose of this formula is to exclude the following rows from [TeamStatus] column (i.e. in the [TeamStatus] column is having "Lost", "Won't proceed", "Next Level", "Disqualified" texts) then the output should be zero and then calculate the spent amount for all other categories in [TeamStatus] column and another condition (i.e. if the year is current year) is met.
I have been trying from past 1 day to make this formula work and no luck. I also tried using FORMAT() function and still it is not working.
Can someone please help me?
The error is saying that your formula should either return a number or text but not sometimes a number and sometimes text.
Notice that your formula sometimes returns "0" and sometimes returns 0.
Either change the first two zeros to be numbers instead of text or else change the last zero to text and use FORMAT on [Spent Amount] to convert it to text.
As a side note, can't your formula be simplified to just?
IF (
AND ( YEAR ( Table2[Match Date] ) = YEAR ( TODAY () ), [TeamStatus] = "Won" ),
[Spent Amount],
0
)

Searching for DAX formula - equivalent to Excel SUMIF

I am searching for a DAX formula.
Specifically:
If SOLD (1st column) = count volume (second column), if not SOLD = 0
I need to reflect the volume of SOLD in a new column. UNSOLD Volumes should be 0.
I attach the reduced data set.
If I understand, you want to only perform aggregation when the SOLD_UNSOLD column is equal to "SOLD", otherwise return 0? If so, the following formula will do this, you'll just need to update the column names accordingly. The outer IF prevents problems resulting from further evaluation (i.e. grand totals) and it's necessary to wrap the column in the VALUES function as this will turn the column into a table of the unique values of it.
'YourTable'[CountOfSold] =
IF (
COUNTROWS ( VALUES ( YourTable[SOLD_UNSOLD] ) ) = 1,
IF (
VALUES ( YourTable[SOLD_UNSOLD] ) = "SOLD",
COUNT ( YourTable[ColumnToAggregate] ),
0
),
0
)
The Excel SUMIF is closest to the DAX SUMX.
I'm not positive I'm understanding what you are asking for, but I think you'd want something like this:
SOLD_VOLUME = SUMX(Table1,
IF(Table1[SOLD_UNSOLD] = "SOLD",
Table1[DAILY_VOLUME],
0
)
)
You could also do this with a filter:
SOLD_VOLUME = SUMX(
FILTER(
Table1,
Table1[SOLD_UNSOLD] = "SOLD"
),
Table1[DAILY_VOLUME]
)

Calculating Variance and Variance% in Dax

In my PBIX File, I have measures that calculate Revenue, COGS, Gross Margin etc.
Revenue = Sum(Amt)
More measures that calculate value for Last year Revenue_LY, COGS_LY and GM_LY.
Revenue_LY = CALCULATE (
[Revenue],
FILTER (
ALL ( 'Date' ),
'Date'[FinYear]= MAX ( 'Date'[FinYear] ) - 1 && 'Date'[FinPeriod] = max('Date'[FinPeriod])
)
)
Now I need variance and variance% measures for each which compare data against last year and budget. The amount of measures is just getting too many.
Revenue_CY-LY = CALCULATE([Revenue],KEEPFILTERS(Versions[VersionCode] = "Act")) - CALCULATE([Revenue_LY],KEEPFILTERS(Versions[VersionCode] = "Act"))
Revenue_CY-LY% = IF([Revenue_CY-LY] < 0, -1, 1) *
IF(
ABS(DIVIDE([Revenue_CY-LY],[Revenue])) > 99.9,
"n/a",
ABS(DIVIDE([Revenue_CY-LY],[Revenue])*100)
)
Is there a way to summarize the measures used. I don't want to create individual measures of each variance.
Yes. You can create a dynamic measure.
First create Revenue, COGS, Gross Margin, etc. measures.
Revenue = SUM([Amt])
COGS = SUM([Cost])
Gross Margin = [Revenue] - [COGS]
...
Then you create a table with one row for each of your measures:
My Measures = DATATABLE("My Measure", STRING, {{"Revenue"}, {"COGS"}, {"Gross Margin"}})
The names don't need to align with your actual measures, but they will be displayed so make them presentable.
Then you create a measure on that table which will dynamically be the same as the selected row in the table:
Selected Measure = SWITCH(SELECTEDVALUE('My Measures'[My Measure], BLANK()), "Revenue", [Revenue], "COGS", [COGS], "Gross Margin", [Gross Margin], BLANK())
Next you go and create all the complicated time-intelligence measures using the [Selected Measure] as the base:
Dynamic_LY = CALCULATE (
[Selected Measure],
FILTER (
ALL ( 'Date' ),
'Date'[FinYear]= MAX ( 'Date'[FinYear] ) - 1 && 'Date'[FinPeriod] = max('Date'[FinPeriod])
)
)
And then you can do [Dynamic_CY-LY] and [Dynamic_CY-LY %] in a similar manner to the ones in your question, replacing references to the [Revenue] measure with references to the dynamic measures.
Now you can either use a slicer on the 'My Measures'[My Measure] column to dynamically change every instance of [Dynamic_CY-LY] and the other dynamic measures, or you can add a filter on each visualisation to filter 'My Measures'[My Measure].
It might be that you'd also like to have a default value for [Selected Measure] instead of defaulting to BLANK(); just put that in last position in the SWITCH() function.

DAX average including zeroes

My question is that I'd like to calculate a daily average taking into account days with zeroes.
Let me clarify it:
I'd like to calculate the average daily value of units for each category, with the following table:
When I sum up the values for each day and category, I get:
I'd like to include in the average calculation the zeroes.
I use the following code:
SUMMARIZE(
Data,
Data[Category],
"Average",
AVERAGEX(
SUMMARIZE(
Data,
Data[Date],
"Sum of Units",
SUM(Data[Units])
),
[Sum of Units]
)
)
But the problem is that for category B it doesn't take into account those days with 0s.
Could you please guide me how to solve it?
Thanks in advance!
Jorge
One way to solve it would be to create a calendar table, which can then be used to count the number of days in any of your grouping periods. This also means you can use non standard calendars, for example something like a 4-4-5
With a calendar table created you can leverage the FIRSTDATE and LASTDATE functions in DAX.
I recommend adding a past dates column to the calendar table, which can be created using DAX with the following formula. The today function in DAX when used in a calculated column will only evaluate when the model is updated.
In my example I created a calculated column in the date table called Past_Dates
Past_dates =IF( TODAY() > [Date], TRUE(), BLANK())
So for example if I create the following measures, the Today one just being used for illustration.
Start_date:=FIRSTDATE( Dates[Date] )
End_Date:=LASTDATE( Dates[Date])
Today:=TODAY()
EndPhased:=CALCULATE( LASTDATE( Dates[Date] ), Dates[Past_dates] = TRUE())
Which when added to an empty pivot table evaluate to the following.
Note that you would want to have year somewhere in the pivot if you have multiple years of data.
The idea of having the Past flag is to keep from counting days where they would not be any data due to being in the future. So for example in September it would only use 11 days of sales and not the full 30.
As the below example shows, the finding of the start and end date even work on a Quarter basis.
So now that we have a way to get the Start and End date of a period, the next step is adding it into our Calculated measure.
In the below example, we are iterating though every unique Category name. Within the category, we are summing the units sold, and then dividing by the number of days between EndPhased and Start_Date + 1. Then averaging the results by the number of categories that have data in that period.
Average:=AVERAGEX (
VALUES ( Data[Category] ),
CALCULATE ( DIVIDE ( SUM ( Data[Units] ), [EndPhased] - [Start_date] + 1 ) )
)
It seems to me that you need to have an underlying row with the zero value in it (in your initial Data table). Right now, you don't actually have a zero value for B on 02/01/2017. If you add a row with the values | 02/01/2017 | B | 0 | I believe you will see that the average accounts for it. As things stand for you right now, I believe the pivot table actually reflects no value (blank) rather than zero value, so the zero isn't currently counted for the average.
I think the best way is to add the missing zeroes, using UNION function:
SUMMARIZE(
Data,
Data[Category],
"Average",
AVERAGEX(
UNION(
SUMMARIZE(
Data,
Data[Date],
"Sum of Units",
SUM(Data[Units])
),
ADDCOLUMNS(
EXCEPT(
ALL(Data[Date]),
VALUES(Data[Date])
),
"Sum of Units",
0
)
),
[Sum of Units]
)
)

Calculated Measure Based on Condition in Dax

I have a requirement in Power Pivot where I need to show value based on the Dimension Column value.
If value is Selling Price then Amount Value of Selling Price from Table1 should display, if Cost Price then Cost Price Amount Should display, if it is Profit the ((SellingPrice-CostPrice)/SellingPrice) should display
My Table Structure is
Table1:-
Table2:-
Required Output:-
If tried the below option:-
1. Calculated Measure:=If(Table[Category]="CostPrice",[CostValue],If(Table1[category]="SellingPrice",[SalesValue],([SalesValue]-[CostValue]/[SalesValue])))
*[CostValue]:=Calculate(Sum(Table1[Amount]),Table1[Category]="CostPrice")
*[Sales Value]:=Calculate(Sum(Table1[Amount]),Table1[Category]="SellingPrice")
Tried this in both Calculated Column and Measure but not giving me required output.
Cost:=
CALCULATE(
SUM( Table1[Amount] )
,Table1[Category] = "CostPrice"
)
Selling:=
CALCULATE(
SUM( Table1[Amount] )
,Table1[Category] = "SellingPrice"
)
Profit:=
DIVIDE(
[Selling] - [Cost]
,[Selling]
)
ConditionalMeasure:=
IF(
HASONEFILTER( Table2[Category] )
,SWITCH(
VALUES( Table2[Category] )
,"CostPrice"
,[Cost]
,"SellingPrice"
,[Selling]
,"Profit"
,[Profit]
)
,[Profit]
)
HASONEFILTER() checks that there is filter context on the named field and that the filter context includes only a single distinct value.
This is just a guard to allow our SWITCH() to refer to VALUES( Table2[Category] ). VALUES() returns a table of all distinct values in the named column or table. So, a 1x1 table can be implicitly converted to a scalar, which we need in SWITCH().
SWITCH() is a case statement.
Our else condition in the IF() is just returning [Profit]. You might want something else, but it's unclear what should happen at the grand total level. You can leave this off, and the measure will be blank in IF()'s else condition.
I was thinking about this a little. I'm not sure why you have your categories on rows. Usually the data set would have columns like: item | CostPrice | SellingPrice | Profit. Then you can just use the columns to define your fields. The model becomes easier and more maintainable.

Resources