I'm trying to count days between a date from the column 'completionDate' and today
The table name is 'Incidents (2)'
I have a simuler table called 'incidents' here it's working.
The code:
DaysClosed = DATEDIFF('Incidents (2)'[completionDate].[Dag];TODAY();DAY)
The error i get:
'A single value for variaton 'Dag' for column 'completionDate' in table 'Incidents (2)' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.'
The error you get strongly depends on how you are evaluating your formula, that's why it might work on another table but not on this one. As #JBfreefolks pointed out correctly you are specifying a column where a scalar value is expected. That can work depending on the context you are evaluating your formula over (assuming it is a measure).
For instance, imagine a data-set with 100 rows equally divided into four categories A,B,C,D. When you create a table visual with a row for each category, each row will have 25 underlying records that will be used in any calculation added to this row (either a measure or an aggregate of any value). This means that when using a formula like datediff with a column reference, it will get 25 values for it's second argument where it expects one.
There are several ways to solve the problem depending on your desired result.
Use a measure like MAX like #JBfreefolks suggested to make sure that a single value is selected from multiple values. The measure will still be calculated over a group of records but will summarize it by taking the maximum date.
Make sure the visual you are using has something like an ID in there so it doesn't group, it displays row context. Any measure added to this visual will evaluate in row context as well.
Use a calculated column instead. They are always evaluated in row context first and their values can be aggregated in a visual later on. When using TODAY() , you probably need to refresh your report at least every day to keep the column up to date.
A more complicated way is to use an iterator like SUMX() or AVERAGEX() to force row context evaluation inside of a measure.
Good to see you solved it already, still posting as it might be helpful to others.
'Incidents (2)'[completionDate].[Dag] referencing a colomn. It is in your computation returning a table (multiple date in the evaluation context) instade of a scalar needed in DATEDIFF calculation.
You need to leverage to be sure that 'Incidents (2)'[completionDate].[Dag] is rturning a scalar value. To do that you can leverage rowcontext and then also formula like Max.
Related
I have a table with 2 measures - One and Two. Both uses CALCULATE and have a filter. There's also a slicer. While Two rightfully ignores slicer's filter context, One doesn't.
'Years'[Year] is a Whole number.
One = CALCULATE(SUM(Years[Sales Amount]),FILTER(Years, 'Years'[Year] = 2010))
Two = CALCULATE(SUM(Years[Sales Amount]),'Years'[Year] = 2010)
What am I missing (in my learning)? I understand Microsoft recommends to NOT use FILTER as filter argument. I'm merely trying to get a grip on the way it works.
There are some tricky details on CALCULATE that produce different results. I’ll show the differences and provide examples below.
Measure One
One = CALCULATE(SUM(Years[Sales Amount]),FILTER(Years, 'Years'[Year] = 2010))
First, remember that every language calculates from the most internal to the most external function.
It means that Power BI will first calculate the function FILTER, which will return a table that contains only the year 2010. Only after that Power BI will perform CALCULATE.
A tricky part here: If there is a filter in the 2nd parameter, the CALCULATE can ignore the slicer (more details on measure two). However, there is no filter on the 2nd parameter, there is only a table! (previously calculated, which contains only year 2010 now).
Since there is no filter, CALCULATE applies the context filter (the slicer in your example) excluding anything before 2013.
Given that, no rows match the filters for Measure One and the result will be blank (if the slicer is set to 2013-2018, of course).
Measure Two
Two = CALCULATE(SUM(Years[Sales Amount]),'Years'[Year] = 2010)
Measure Two do have a filter expression: 'Years'[Year] = 2010
When filter expressions are provided, the CALCULATE function changes the filter context (which contains slicers or filter on page etc.) to evaluate the expression.
For each filter expression, there are two potential outcomes in CALCULATE:
Outcome A. If the columns are already in the filter context, the existing filters will be overwritten by the parameter in the CALCULATE expression. I.e., does not matter the slicer (or filter on this page or on all pages…)
That is the case for your measure two!
Since you are using 'Years'[Year] in both the CALCULATE expression and the slicer, the context filter will be overwritten by the filter in the CALCULATE expression. I mean, the result is ignoring the slicer/filter context and you will find "3" as the result.
Outcome B. If the columns aren't in the filter context, then new filters will be added to the filter context to evaluate the expression. In other words, it will consider both the slicer/filter context and the filter expression in CALCULATE.
To exemplify this behavior, I have created a new measure:
ThreeSalesFilter = CALCULATE(SUM(Years[Sales Amount]), 'Years'[Sales Amount]=3)
It keeps the exact same row as measure two, right? The Year=2010 is the same row Sales Amount=3.
However, the result of this measure is blank and not 3. Quite interesting!
Again, it happens because CALCULATE is using 'Years'[Sales Amount] but the slicer/filter context is using 'Years'[Year] – another column. Therefore the CALCULATE filter "joins" the filter context; do not overwrite it.
Below, the reproduction of the dashboard including my third variable.
If you need more details or clarifications, please let me know in the comments and I'll edit here.
You are getting different behaviors because of the way that the CALCULATE function works with filter context; you are passing two different types of arguments into the calculate function.
In measure one you are passing a table filter expression. It takes the table created by the FILTER function, and then applies the filter context given by the slicer. Table filter expressions do not override the existing filters on any columns.
In measure two you are passing a Boolean filter expression as the second argument. It overrides the filter context since they both filter based on the year column, therefore ignoring the slicer.
MS best practices do caution against using FILTER as a filter argument, but they do also mention examples of several times where it is appropriate.
I try to create a table like yours
also create the two measures which build on the dashboard
So, I think the values displayed by One and TWO are not the same as the DAX syntax. But your One is BLANK that the possible reason is that One caught the filter condition on the right side of your way, while TWO caught the value of the table on the left, I guess it is because it's not the same.
I'm trying to use a previous calculated Measure in a Calculated Column in PBI, but i get the error "expressions that yield variant data-type cannot be used to define calculated columns", how can i fix this? this is my code
Critical_Detrended = 1*(1+[Trend])
Thanks
Calculated columns are static. They are calculated once, when the data is loaded. Their values cannot change. Thus, you can't use something dynamic, like measures, because measures are responsive, they are re-computed when the user selects something, changes the value of a slicer, etc.
The solution in your case is to change your calculated column to be a measure too.
You could take a look for example at Calculated columns and measures in DAX article.
While calculated columns are not responsive to any slicers or filters set on your report, you absolutely can use measures in a calculated column definition so long as you understand how the row context to filter context transition works and don't expect the calculated column to be dynamically responsive.
That said, the error you are getting sounds like [Trend] may output different data types in different situations but a column can only be a single data type. A non-obvious case where this sort of thing happens is when a measure sometimes outputs an integer and other times outputs a decimal type. In a case like that, you can force it to return a decimal type by multiplying by 1.0 or adding 0.0 or similar manner of type coercion. In your case, changing one of the 1 to 1.0 might do the trick if that's indeed the problem.
I have created a parameter which has values 1-20.
Now I want to use this in a calculated column , wherein the calculations is
if(Parameter[Parameter Value]>'GPH Activity'[Idle Time 2],1,0)
Here Idle time is a number field.
This however doesn't seem to work. What I am doing wrong?
You can't use parameters in Calculated Columns, only in Measures.
Calculated Columns are calculated when the data model is refreshed, they're not dynamically recalculated as the report is consumed.
Decide how you wish to aggregate this calculation, and write an appropriate measure instead. You may wish to use an Iterator function, such as COUNTX or SUMX.
I have an Entity column with one row per entity. This table has three columns: Entity ID, a Descriptor, and a Metric. The Descriptor is a concatenation of numerous codes and I would like to see the metrics broken down by code.
I originally just split the Descriptor column into numerous rows but that led to some data relationship issues so I'd like to do it without splitting the Descriptor column.
I tried doing the following DAX formula but it resulted in an error stating "the expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression"
Desired Output Metric = CALCULATE('Metric',CONTAINSSTRING('Entity Table'[Descriptor],'Code Table'[Code]))
Ultimately I'm not even sure I need this as a column, and it may be better as a measure...
Any help would be appreciated. Thank you!
You can get around "the expression contains multiple columns, but only a single column can be used in a True/False expression that is used as a table filter expression" by using Filter within your CALCULATE.
Here it is as a created column. I used an IF because 'E' code evaluates to a blank and you wanted a 0.
Desired Output Metric = IF(CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],'Code Table'[Code])))>0,CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],'Code Table'[Code]))),0)
Here it is as a measure. Be careful to only use this at the Code detail level. When making a measure you need to use aggregate functions to reference your columns, so I am just doing the MIN(Code) since for any single code the Min() will always evaluate to equal that Code. If you try to use this at a higher summary level you may get some odd answers as it will only total for the MIN() code in the data set you are referencing.
Desired Output Metric = IF(CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],MIN('Code Table'[Code]))))>0,CALCULATE(SUM('Entity Table'[Metric]),FILTER('Entity Table',CONTAINSSTRING('Entity Table'[Descriptor],MIN('Code Table'[Code])))),0)
I am facing a problem right now that I can’t find the answer to in any online research.
I have a calculated measure as follows…
top_selected_tier:=CALCULATE(
min(Table1[waterfall_tier]),
ALLSELECTED(Table1)
)
…that shows the correct value in the measure pane, after slicing the [waterfall_tier] field (using either slicers or filter dropdowns).
However, when I reference that measure in a calculated column…
[test_top_tier]=[top_selected_tier]
… I get a different value. It essentially returns the lowest value in the table, irrespective of which set of filters/slicers are applied.
What I need to test for is whether the field [waterfall_tier] at the row level matches the currently lowest value for [top_selected_tier] that is selected via slicers as reported in the measure pane.
Similarly, in another calculated column I am trying to do a sum of all “filled” requests in the table for a given date, and I don’t know how to construct the formula to only sum the selected values (via slicer/filter dropdown). Instead this formula sums all the values for that day, irrespective of the set of filters/slicers applied.
[all_filled]=CALCULATE(
sum([requests]),
FILTER(ALLEXCEPT(Table1,Table1[request type],Table1[date]),
[request type]="filled"
)
)
In effect I need the formula to return an ALLEXCEPT on the ALLSELECTED subset of table… if that makes sense?
Please note the features of the CALCULATE:
New filter context is added.
The function transform existing row contexts into an equivalent filter context.
The preceding simultaneous occurrence.
Calculated columns are not dynamic, that's why you are getting a fixed value. (see this article)
One important concept that you need to remember about calculated columns is that they are computed during the database processing and then stored in the model.
This means that the column value is evaluated only once, in the beginning, when there are no filters yet, and therefore the "ALLSELECTED" dataset is the whole table.
In PowerBI (sadly) you cannot create a dynamic column.