How to use GROUPBY function in PowerBI? - powerbi

I tried using group by DAX function in PowerBI as Measure, New Column, New Table but I get an error in validating the function,
New Table = GROUPBY(
'tab1',
'tab1'[color],
"Group By color",
sum('tab1'[count_lables])
)
Error : Function 'GROUPBY' scalar expressions have to be Aggregation functions over CurrentGroup(). The expression of each Aggregation has to be either a constant or directly reference the columns in CurrentGroup().

The error says you need to use an aggregation function over a group, you are using SUM function which doesn't sum values over any group. In your case you need to use SUMX aggregation function with CURRENTGROUP() option. The CURRENTGROUP determines the groups that must be added based on the current row.
Try using the below expression instead.
New Table = GROUPBY (
tab1,
tab1[color],
"Group by color", SUMX ( CURRENTGROUP (), tab1[count lables] )
)
Let me know if this helps.

Related

dax difference between filter vs filter values

Can someone explain how the first filter statement is different from second in DAX?
FILTER(Table, Table[Column] = "some text")
FILTER(VALUES(Table[Column]), (Table[Column] = "some text"))
The FILTER function is a table function, meaning it will return a table. In the case of your second example, it is likely that you will get a scalar value (a single value) because you are filtering a table (of one column of unique values) by a single value. In the first FILTER instance, however, you will be returning an entire table of the first argument, which has only been filtered by the conditional in the second argument. For reference, I have used the sample data built within the Power BI Desktop version to show you the key differences.
From your first FILTER example
FILTER( financials, financials[Country] = "Germany" )
Output
From your second FILTER example:
FILTER( VALUES( financials[Country] ), financials[Country] = "Germany" )
-- The extra parantheses around the second argument are not necessary...
Output
Therefore, the main functional difference is in the output. What are you wanting to return? An entire table or a specific value(s) of a column(s)?

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

Does FILTER in CALCULATE add or modify the filter context?

In CALCULATE we can use FILTER(tblname,..)
Or
FILTER(ALL(tblname),..)
I want to ask whether FILTER in CALCULATE adds or modifies (clears and adds new) the existing filter context?
The answer is yes. It adds a new filter when no filter exists on tblname or it replaces (removes the existing filter and adds the new filter) when a filter already exists on tablename or some of its expanded table columns when CALCULATE is called. Existing filters on non-related columns are unaffected.
apart from the first parameter in CALCULATE, that is the expression to be computed in the modified filter context, all the other parameters are called filter arguments, since their purpose is to change the filter context.
CALCULATE( expression, table[column] = value )
is internally translated by DAX to
CALCULATE( expression, FILTER( ALL( table[column] ), table[column] = value ) )

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.

PowerBI DAX get COUNT DISTINCT with GROUP BY , see SQL query below

I have got this following SQL query that gives me the correct value from the database.
SELECT
SUM( DISTINCT_ORDER_NUMBERS )
FROM
(
SELECT STORE_KEY,
COUNT( DISTINCT TRANSACTION_NUM ) AS DISTINCT_ORDER_NUMBERS,
DATE_KEY,
TRANSACTION_TYPE_KEY
FROM Pos_Data
GROUP BY STORE_KEY,
DATE_KEY,
TRANSACTION_TYPE_KEY
)
AS A
I am however facing challenges writing a DAX formula for a measure in Power BI Here is what I have tried so far but I get an error.
Total Number Of Orders
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUM(_TotalOrders[DISTINCT_ORDER_NUMBERS])
Please assist
The SUM function expects a base table rather than a calculated table.
Try this instead:
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUMX(_TotalOrders, [DISTINCT_CHECK_SEQ])
Edit: If the difference you mentioned is related to nulls, then try this in place of DISTINCTCOUNT.
COUNTAX( DISTINCT( 'Pos_Data'[TRANSACTION_NUM] ), 'Pos_Data'[TRANSACTION_NUM] )
The COUNTAX function (as opposed to COUNTX) does not count nulls.