FILTER vs CALCULATE DAX Function - dax

Is there a difference between the FILTER and CALCULATE Dax functions?
From what I understand, they produce the same result and are synonymous.
Is this true?

They aren't remotely the same.
FILTER takes a table expression as its first argument and iterates through all the rows of that table checking the condition provided in the second argument. It returns a table.
CALCULATE calculates a scalar expression (first argument) in the filter context determined by the remaining arguments. It returns a single scalar value.

Related

Find the most and least values of 8 calculated fields

I have 8 calculated fields in Tableau and I need to find the lowest 2 of them and highest value. How do I do that? is that possible in Tableau?
There are actually two functions named MIN() -- the same applies to MAX(), but I'll just talk about MIN().
The most commonly used takes a single argument - and is an aggregate function, such as MIN([Sales]). It operates on multiple records and returns the minimum value found in the whole set of records. It is translated into the SQL MIN() function for relational data sources.
The other version of MIN() takes two arguments, such as MIN([x], [y]). It is a record level function and operates on a single record and returns the minimum value of its two arguments for that record. It is translated into the SQL LEAST() function for relational data sources.
You want to use the second form. Unfortunately, Tableau restricts it to two arguments, but you can simply nest multiple calls as in MIN(MIN(a, b), c)

PowerQuery syntax to overcome #NUM! error

I have two columns of data in Excel. Using PowerQuery I am trying to divide these two columns and call it column X. The problem is that there are zeros in these two columns meaning that we get a "#NUM!" in Column X when dividing. How can I write an IF statement in PowerQuery so that IF the value of column X (the division) is Nan (#NUM!) then it is set to zero?
The below doesn't change the NaN's to zeros:
if[Column1]/[Column2]="NaN" then 0 else[Column1]/[Column2]
This should be a FAQ but approach is similar in almost every langage. I'd write your statement like this: if [Column2] = 0 then 0 else [column1]/[column2]. Should work for all non-zero denominators.
Other thought, I just used this: Powerquery (and PowerPivot) has a divide function that is divide-by-zero-safe! divide(column1,column2). Shorter to write and should perform better as it is only performing the calculation once. Especially with more complex denominators.
Final thought: because they aren't additive, I tend not to store ratios in the PQ results choosing instead to calculate dynamically in powerpivot or elsewhere in the reporting. In Excel you can use =iferror(a/b, 0).
JR

Is there any option to do FOR loop in excel?

I have an excel that I'm calculating my Scrum Task's completed average. I have Story point item also in the excel. My calculation is:
Result= SP * percentage of completion --> This calculation is for each row and after that I sum up all result and taking the summary.
But sometimes I am adding new task and for each task I am adding the calculation to the average result.
Is there any way to use for loop in the excel?
for(int i=0;i<50;i++){ if(SP!=null && task!=null)(B+i)*(L+i)}
My calculation is like below:
AVERAGE((B4*L4+B5*L5+B6*L6+B7*L7+B8*L8+B9*L9+B10*L10)/SUM(B4:B10))
First of all, AVERAGE is not doing anything in your formula, since the argument you pass to it is just one single value. You already do an average calculation by dividing by the sum. That average is in fact a weighted average, and so you could not even achieve that with a plain AVERAGE function.
I see several ways to make this formula more generic, so it keeps working when you add rows:
1. Use SUMPRODUCT
=SUMPRODUCT(B4:B100,L4:L100)/SUM(B4:B100)
The row number 100 is chosen arbitrarily, but should evidently encompass all data rows. If you have no data occurring below your table, then it is safe to add a large margin. You'll want to avoid the situation where you think you add a line to the table, but actually get outside of the range of the formula. Using proper Excel tables can help to avoid this situation.
2. Use an array formula
This would be a second resort for when the formula becomes more complicated and cannot be executed with a "simple" SUMPRODUCT. But the above would translate to this array formula:
=SUM(B4:B100*L4:L100)/SUM(B4:B100)
Once you have typed this in the formula bar, make sure to press Ctrl+Shift+Enter to enter it. Only then will it act as an array formula.
Again, the same remark about row number 100.
3. Use an extra column
Things get easy when you use an extra column for storing the product of B & L values for each row. So you would put in cell N4 the following formula:
=B4*L4
...and then copy that relative formula to the other rows. You can hide that column if you want.
Then the overal formula can be:
=SUM(N4:N100)/SUM(B4:B100)
With this solution you must take care to always copy a row when inserting a new row, as you need the N column to have the intermediate product formula also for any new row.

DAX Group By DateDiff Segments

I am writing a DAX query for use in SSRS dataset.
I have a requirement to calculate the date difference between a dimension date column and today(), then group these into segments, I.e. 1-7 days, 7-10 days etc - and return the segment for each row in the table (with other additional filters)
As it’s for use in SSRS I’m using EVALUATE(SUMMARIZECOLUMNS
A switch statement won’t work for value ranges in DAX and when I use nested If statement the query uses all available memory.
Is there a function I can use for to group by the calculated column into segments.
Any advise appreciated.
Thanks
W

Finding 2nd maximum in crosstab of Cognos Anlytics

I am using Cognos Analytic v11 (with a dimensional DB).
I have a table showing the 24-hour readings of sensor A, B and C. Finding the maximum in crosstab is easy using the Summarize function; but how do I find the 2nd maximum (i.e 2nd highest)?
I was able to get this to work for me:
aggregate([{Amount Field}] within set subset(order([{Sensor Field}],[{Amount Field}],DESC),1,1))
Replace {Amount Field} with the name of the actual amount data item and {Sensor Field} with the item you want to find the 2nd maximum within.
The order function sorts the set by the amount field. The first member of the resulting set is the maximum. The second member of the resulting set is the 2nd maximum. We isolate the second maximum by using the subset function to get the member with index 1 and then we aggregate the measure for this one member.

Resources