How to use sum or sumif in an "ifelse" aggregation - amazon-web-services

I am using Amazon QuickSight and having troubles calculating a field in a table. I need to use an "ifelse" aggregation where the "if" and "else" are sums of other fields in the table. I keep getting the following error and don't understand how to correct it:
mismatched aggregation, custom aggregations cant contain both aggregate SUM and non - aggregated fields
Code example:
ifelse(
({calculated field} = "Won"),(sumIf({amount},(({field}="Won"))
))
,({calculated field} = "Not Final"),({sum_amount_2})
,1
)

Related

Amazon QuickSight - Display number

I am trying Amazon Quicksight but I don't understand if this is possible.
I should display a number that is calculated:
[(a-b) / c]
a - is chosen from a list of data in the column A
b - is the mean of the column B
c - is the mean of the column C
it's possible?
Thanks
Where a differs depending on the row in column A? I don't think this is possible as you are writing a formula using both aggregated fields (mean of b or c) and a non-aggregated field (a).
I tried the formula with both and got the following error (using the avg function):
Mismatched aggregation. Custom aggregations can’t contain both
aggregate "AVG" and non-aggregated fields “AVG("ColumnId-2")”, in
any combination.
#Occamatic is right about inability to use both aggregated fields and a non-aggregated field in your formula.
However, you can circumvent this by using 'a' in an aggregated function in your calculated field. Example:
( sumIf({a},{a}={a}) - b ) / c
Please amend to the specifics to your dashboard, possibly with use of parameters in ifelse statements, but a version of this should work.
For instance, I myself can't use:
ifelse({metric_type}='Averages',avg({metric_value}),sum({metric_value}))
Instead I use:
ifelse(avgIf({metric_value},{metric_type}='Averages') > 0,avg({metric_value}),sum({metric_value}))

Why using ALLSELECTED() AND FILTER() together?

I am trying to understand the below DAX code:
CALCULATE(
SUM(Revenue[Net])
,FILTER('Center', NOT 'Center'[Acc] IN {"RSM", BLANK() })
,ALLSELECTED()
,VALUES('Customer'[Customer Number])
)
I have the below questions:
What's the use of ALLSELECTED?? By definition ALLSELECTED returns all rows in a table, ignoring any filters that might have been applied inside the query, but keeping filters that come from outside. https://dax.guide/allselected/
So, what's the point of writing FILTER() if its going to be forced to be ignored by the next line (ALLSELECTED)?!?
Also by definition:
CALCULATE is just a expresion followed by filters...
What's the use of VALUES() ? It doesn't appear to be a filter, so how is it even allowed to appear there? (Per definition VALUES(): returns a one-column table that contains the distinct values from the specified column.)
I do not understand what is this returning? is it the SUM() or the VALUES()?
(I come from a SQL background, so any sql-friendly answer is appreciated).
In Dax every filter is a table of values its look similar to INNER JOIN;
ALLSELECTED is useful when you need to keep a row context (this is also a filter in DAX). You can use ALLSELECTED inside FILTER function.
For better understand what engine does you can use a DaxStudio with ServerTiming;
As you see this product one simple statement:
SELECT
SUM ( 'Table'[Cost Centre] )
FROM 'Table'
WHERE
'Table'[Project] NIN ( 'AB' ) ;
You can find useful article by Alberto Ferrari and Marco Russo:
https://www.sqlbi.com/tv/auto-exist-on-clusters-or-numbers-unplugged-22/
If it is only converting DAX queries if your connecting your sql analysis services to in DAX studio. because it is not working for PBI file data

Unable to divide two column in quicksight

I have made two calculated field in my quicksight anaysis - 'Correct Prediction' and 'Total Count'
Formulas used for above two calculated are -
Correct Prediction = ifelse({best_exp_rank} <= 15,1,0)
Total Count = count({best_exp_rank})
Now I made a pivot table and kept the above columns as values(Correct Prediction(Sum), Total Count(custom)).
When I'm trying to make a third column which is (Correct Prediction/Total Count) as a calculated field.
I face the following error.
mismatched aggregation. custom aggregations can’t contain both
aggregate "count" and non-aggregated fields
“count("best_exp_rank")”, in any combination.
Can anyone please help me with this problem? Please comment if you need any extra information
Add a calculated field like -
(sum({Correct Prediction}) / {Total Count}) * 100

Issue in applying filter in Power BI Desktop DAX forumla

I am facing an issue while working with power bi desktop. i am having an csv file as a datasource and i am having a column with multiple values, say "abc, def", "jkl" "zyz" etc.
Here, i need to generate a report with rows having showing "def" & "jkl" .
How to filter this using DAX Filter command. i wanted to fetch, filter only two values CTF& EVE in the Power BI report.
I tried with creating a calculated column and applied the below code but it didnt work:
Columnjkl = FILTER((Table1,OR(Table1[mycolumn1] == "def" || "jkl"))
filter-2cols-ctf-eve-
You need to read how DAX syntax is written, check this page to see how you use the FILTER function (FILTER) and this for how to use the OR function (OR)
Basically, Filter returns a table, so using it to calculate a column will not work. However, by implementing the FILTER function inside a CALCULATE you can change/modify what the main argument of CALCULATE will evaluate. The calculation flow in DAX is such that first the filter argument is evaluated and then the aggregation (or what ever) is evaluated. Example:
NumberOfRowsWithABC =
CALCULATE =
COUNTROWS('Table1'),
FILTER(
'Table1',
'table1[mycolumn1] = "abc"
)
)
First the FILTER function only selects the rows in 'Table1' where [mycolumn1] has the text value abc. Then it passes this reduced table to the COUNTROW function, which counts the number of rows in the table passed to it.
The syntax for the OR function is wrong. This is how you should write it:
OR(
'Table1'[mycolumn1] = "def",
'Table1'[mycolumn1] = "jkl"
)
Perhaps a better way of writing this OR function is using the in-command as the filter argument of the calculate function:
NumberOfRowsWith_def_and_jkl =
CALCULATE(
COUNTROWS('Table1'),
'Table1'[mycolumn1] in {"def", "jkl"}
)
But as Gangula wrote, you don't need to filter 'Table1' using the FILTER function, it can all be done visually on the dashboard.

Parsing DAX Query with Regular expression to get measures and dimensions

I want to create an application that uses Extended Events to analyze the usage of Azure Analysis Services models.
Specifically, I want to know what measures and dimensions the end users are using.
I look at the QueryEnd event and try to parse the TextData field. Depending on the tool used for querying the model I get either MDX or DAX in the TextData.
I think I have managed to parse the MDX with this RegEx: ([[\w ]+].[[\w ]+](?:.(?:Members|[Q\d]))?)
(from this post: Regular expression for extracting element from MDX Query)
Now parsing the DAX is the problem. If I query the model from fx PowerBI I get a DAX like this:
EVALUATE
TOPN(
502,
SUMMARIZECOLUMNS(
ROLLUPADDISSUBTOTAL('Product'[Color], \"IsGrandTotalRowTotal\"),
\"Order_Count\", 'SalesOrderDetail'[Order Count]
),
[IsGrandTotalRowTotal],
0,
[Order_Count],
0,
'Product'[Color],
1
)
ORDER BY
[IsGrandTotalRowTotal] DESC, [Order_Count] DESC, 'Product'[Color]
What I would like to match with the RegEx is:
'Product'[Color] and 'SalesOrderDetail'[Order Count]
And.... how would i know that Order Count is used as a measyre while Color is an attribute of the Product dimension?..... guess I won't?
Thanx a lot
Nicolaj
think I just found a possible solution for parsing both DAX and MDX queries:
([\[\'][\w ]+[\]\']\.?[\[\'][\w ]+[\]\'])(?!.*\1)
This will give me what I need.... without duplicates. Improvements are welcomed :-)
For disambiguating columns from measures, I would query the DMVs for the deployed model to get a list of columns and a list of measures. Then you can just look up your parsed tokens in the two lists:
DMV docs
Column DMV
Measure DMV.
Note that measure names are globally unique among the population of column and measure names, so that is an easy lookup (just throw away the table reference). Column names are only unique within the table they belong to.
It looks like you've already got a regex that's working for you, so run with that.
I'll also note that almost all PBI visuals which are returning anything other than a simple list (e.g. slicers or one column tables will return just a list) will follow the pattern of the query you shared.
Measures should all be in later args to SUMMARIZECOLUMNS. The pattern is:
SUMMARIZECOLUMNS (
<grouping columns, optionally in a ROLLUPADDISSUBTOTAL>,
<filters, defined above in VARs>,
<measures as ("Alias", [Measure]) pairs>
)