Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli


Understanding DAX Logical Operators: AND, OR, and Filtering in Power BI - Prof. Bergamasch, Sbobinature di Sistemi Informatici

An in-depth explanation of the dax logical operators and and or, their usage, and how to apply them to filter data in power bi. Learn how to create measures with multiple conditions and mix and and or operators in the same code.

Tipologia: Sbobinature

2023/2024

In vendita dal 14/03/2024

meg-venturi
meg-venturi 🇮🇹

2 documenti

1 / 5

Toggle sidebar

Questa pagina non è visibile nell’anteprima

Non perderti parti importanti!

bg1
LECTURE 06/03/2024
Duplicate the page we have in Power BI and leave only the measures Sales to Bachelors
and Sales.
If we click on the measure applied in the data list, we can see its code
We are looking at the Sales to Bachelors measure. We know that with this code, we have
visible rows of Sales filtered by color and then also by the condition
EnglishEducation=Bachelors.
What if I want to see the sales to Bachelors in 2003?
in order to check more than one condition, we need to use a LOGICAL OPERATOR→ it is
an operator/function whose output is a logical value, which is either TRUE or FALSE.
We’re studying the principal two: AND & OR
The AND function/operator gives the output TRUE only if all the conditions we gave it as
input are true at the same time.
The OR function/operator always returns TRUE. The only condition in which it returns
FALSE, is when all the conditions we gave it are false at the same time.
In DAX, we can generate functions AND only with 2 conditions. If we need 3 or more
conditions, we need to nest the AND with another AND. The AND operator for 3 or more
conditions is advisable to use, since it doesn’t need to nest multiple AND functions.
Code to see sales to Bachelors in 2003
- start copying the code of Sales to Bachelors
= SUMX(FILTER
(Sales,
RELATED(Customer[EnglishEducation]) = "Bachelors"
)
,Sales[UnitPrice]*Sales[OrderQuantity]
)
- create a new measure in Sales Sales to Bachelors 2003
= SUMX(FILTER
(Sales,
AND(
RELATED(Customer[EnglishEducation]) = "Bachelors",
RELATED(‘Calendar’[CalendarYear]= 2003
)
)
,Sales[UnitPrice]*Sales[OrderQuantity]
)
every row would be included, only if the AND function returns TRUE
The more conditions we put in, the less we'll see, since we’re always more restricted.
ATTENTION= this number must be written without “”
pf3
pf4
pf5

Anteprima parziale del testo

Scarica Understanding DAX Logical Operators: AND, OR, and Filtering in Power BI - Prof. Bergamasch e più Sbobinature in PDF di Sistemi Informatici solo su Docsity!

LECTURE 06/03/

Duplicate the page we have in Power BI and leave only the measures Sales to Bachelors and Sales. If we click on the measure applied in the data list, we can see its code We are looking at the Sales to Bachelors measure. We know that with this code, we have visible rows of Sales filtered by color and then also by the condition EnglishEducation=Bachelors. What if I want to see the sales to Bachelors in 2003? → in order to check more than one condition, we need to use a LOGICAL OPERATOR→ it is an operator/function whose output is a logical value, which is either TRUE or FALSE. We’re studying the principal two: AND & OR The AND function/operator gives the output TRUE only if all the conditions we gave it as input are true at the same time. The OR function/operator always returns TRUE. The only condition in which it returns FALSE, is when all the conditions we gave it are false at the same time. In DAX, we can generate functions AND only with 2 conditions. If we need 3 or more conditions, we need to nest the AND with another AND. The AND operator for 3 or more conditions is advisable to use, since it doesn’t need to nest multiple AND functions. Code to see sales to Bachelors in 2003

  • start copying the code of Sales to Bachelors = SUMX(FILTER (Sales, RELATED(Customer[EnglishEducation]) = "Bachelors" ) ,Sales[UnitPrice]*Sales[OrderQuantity] )
  • create a new measure in Sales → Sales to Bachelors 2003 = SUMX(FILTER (Sales, AND( RELATED(Customer[EnglishEducation]) = "Bachelors", RELATED(‘Calendar’[CalendarYear]= 2003 ) ) ,Sales[UnitPrice]*Sales[OrderQuantity] ) → every row would be included, only if the AND function returns TRUE The more conditions we put in, the less we'll see, since we’re always more restricted. ATTENTION= this number must be written without “”

&& → AND operator, to use instead of the function This is the same code, but with the AND operator. Notice that on top, we need to specify that this && is an operator and which one it is. Exercise: try to write a measure with 3 conditions starting from the code we already have and add the condition ‘Customer’[EnglishOccupation]=”Clerical” → it is sufficient to add another AND operator and another RELATED function OR operator → ||

  • Starting from the Sales to Bachelors 2003 measure, we modify it to write a code that has the conditions Bachelors or happened in 2003 → we don’t want to see only rows that aren’t either made in 2003 nor from a Bachelor. As we can see below, the code is exactly the same as the one with the AND operator, only the operator itself is changing.

Everytime I write a measure, I’m on the reporting side (report view). When we’re in the reporting side we create filters by taking every column we want, we put it in rows, we generate a list of values and each of that will be applied as a filter. The filters will propagate to the fact table and once the propagation is done, measures are calculated on top of the rows that are surviving the filters. Filters decide which subpart of the data will be subject we have written in a measure. When we speak about filter context, we speak about the reporting part (PivotTable, Bar chart, matrix,..). When we speak about measures, calculated in the context of the filters, we have the entire data model in our hands and all the tables. These tables are filtered through the filters we generate on the visuals (that’s why we speak about filter context). ROW CONTEXT: We create a calculated column in the Data View (table view in Power BI) and we’ll go on a single table, since when we create a column, we need to create it in a particular table (we can’t create it in the entire data model).

  • go to Table view if we click on the SalesAmount column, we don’t see a formula in DAX, since it is a native column (it is present in the source). We want to recreate the same values with a calculated column, a column not available in the original source.
  • go on Sales in the Data tab on the right and select “new column”
  • name it “my salesAmount”
  • formula: This is a calculated column evaluated in a row context. → a calculated column is the evaluation of the DAX code that we write in every row one row at a time in the entire table. This process is called ITERATION.

● if we try to use this code for a measure it gives us error. Why does it work for a calculated column and not for a measure? → When we write a calculated column, the DAX code is evaluated in a row context, which means that it's evaluated in a single row at a time. Since it is evaluating a single value at a time and not the entire column, this code can work for a calculated column. Instead, when we’re calculating a measure, we’re taking the entire column and it isn’t possible to multiply entire columns between each other. To make this calculation in a measure, we need to use the SUMX function, which creates a row context on the table we give it as input. For a calculated column, the row context is automatically created. Example: Create a new calculated column in which we want to see three labels based on the value of the UnitPrice: low, medium or high. If the UnitPrice is less than 50, it is low. Between 50 and 200 it is medium. Otherwise I want to see high. To make it work in a measure, we need an iterator to make it work. The Price Category column is useful to create, since it is useful to insert in the report. *Why can we put columns in rows, but not measures in rows? → because columns contain values, instead measures don’t contain any value. The value that the measure calculates live only in the report. With columns we generate a context that returns filters and that will decide which rows of the data will be subject to calculation, done with a measure. Filtering and iterating: When we filter, we can't iterate and vice versa. The filter context filters and doesn’t iterate, the row context iterates and doesn’t filter.