How to do countifs in powerbi - powerbi

I want to do countif in PowerBI (Example: count of total item with condition #1: having transaction of 2 or more, condition #2: year)
I have sample data comprised of PO (transaction) #, item #, date (year), item description, ship to location.
I can achieve the result through Excel - Pivot a matrix table of rows of items, columns of year and the value is count of PO/transactions. A separate table on the side is utilized to count the total item within Column (Years) that has transaction count > 2.
I can't figure out what DAX to use in PowerBI to achieve the same thing as I could with Excel:
Sample of transaction counts:

Usually COUNTIFS in Excel are implemented by use of CALCULATE(<expression>,<filter1>,<filter2>…) in Power BI, for example:
CALCULATE(SUM(Table1[Column1]), FILTER(Table1, Table1[Column1] < Table1[Column2]))
Read more details here.

Related

PowerBI DAX Query - undo filter slicer on certain values based on expression

I have below table structure:
enter image description here
here I want to put a date slicer in Power BI to filter on dates and return the count of total rows as total in the card as shown below:
enter image description here
simple, the only twist is that I want to have the total of hybrid car added at all times.
i.e.
Brands before 5/25/2020 = 4 Hybrid + 1 Electric = 5
Brands before 12/5/2020 = 4 Hybrid + 3 Electric = 7
I have found a solution, which is creating a view in my database, which jus holds the number count of hybrid car (select count(*) from table where cartype = 'hybrid') and using it to sum with filter rows in power bi - but I am looking for a solution completely in Power BI DAX query.
any measure I have tried to create in power bi is filtered by date slicer and so doesn't work.
Create these measure:
TOTALROWS = COUNT('cars'[brand])
ELECTRIC_NUM = CALCULATE([TotalRows],('cars'),'cars'[cartype]="ELECTRIC")
HYBRID_NUM = CALCULATE([TOTALROWS],ALL('cars'),'cars'[cartype]="HYBRID")
TOTALBYBUSINESSLOGIC = CALCULATE([ELECTRIC_NUM]+[HYBRID_NUM])
Now use the last measure (i.e. TOTALBYBUSINESSLOGIC) to be used in your Card to display the total, Notice the expression diffrence between ELECTRIC_NUM and HYBRID_NUM
(In HYBRID_NUM I have used ALL, All will have it bypass the Date Slicer filter) whereas ELECTRIC_NUM will only proivde sum of rows falling in the active date sliver range.

How to create a column in table where the value come from the summation of another column in another table

I have 2 tables in PowerBI.
Table 1:
Table 2:
I have the sales target for each month in Table 1 and also the actual sales that I achieved in Table 2. May I know how should I create another column in Table 1 where it sum all the sales from Table 2 based on respective months? The expected output will be something like this:
Expected output:
Any help or advise will be greatly appreciated!
Here's one potential solution:
Summarize table two so that it groups the total sales by month. You can do this by grouping on the month in power query, or using a dax function like SUMMARIZE.
Create your new column in table 1 using the LOOKUPVALUE function. It will look something like this LOOKUPVALUE(Total Sales, Date, Month & Yrs).
Make a relationship between Table 1 and Table 2 using "Month & Yrs" and "Date".
Then it's just a matter of adding a table with "Month & Yrs" as rows, and "Sales Target" and "Sales" as values. You can then just rename if you want.
I'd recommend using a calendar dimension as a general rule. Much easier to make relationships, add columns etc.

Power BI DAX data from 2 tables

I have 2 tables see image
And I want to get total revenue for Store ABC only for product "ID1" so in this simple tables the final Revenue should be 251.
So I should filter only "ABC" in first table and filter only "ID1" in the second and then add column "Revenue" to the first table with the key "email" and then SUM revenue, but I got completely lost how to write it in DAX
Any suggestions?
Thanks
Lukas

How to show last six months data(one row for each month) in MDX query based on selected month filter in Power BI report?

I have created a cube with one fact table and 5 dimension which includes one Date dimension. Hierarchy of date dimension is Year -> Quater -> Month. Here I want to select the last six months data from selected month using mdx query. The output should be Month (Row Level) and Measures on Column level.
Thought of creating a dynamic named set but power BI doesn't consume named sets.
Can anyone please suggest a way to do it in MDX either using a disconnected date dimension or any idea in Power BI?
Take a look at the sample below
select
[Measures].[Internet Sales Amount]
on 0
,
[Date].[Calendar].[Month].&[2012]&[4].lag(6):
[Date].[Calendar].[Month].&[2012]&[4]
on 1
from
[Adventure Works]
Result

DAX CALCULATE() doesn't seem to be overriding pivot filter

I am learning to use Power Pivot and write DAX and I am working through a book I bought written by Rob Collie and Avichal Singh called "Power Pivot and Power BI".
Early on it explains what happens when using CALCULATE() in a measure. A key point of understanding is explained as follows:
If a filter argument acts on a column that IS already on the pivot, it
will override the pivot context for that column
So in a simple table called "GSR" I have a series of invoices with an invoice date, a product and an amount. I have another column that converts all invoice dates to the last day of the month to gather them together. I have created a measure called "Total orders" that is counting the number of rows.
I have created a pivot of this data with:
Products in the rows
Month end in the columns (but I've actually chosen Month and Year from the automatic breakdown Power Pivot has done on my Month_End column)
Orders in the values
This pivot renders correctly.
Now the issue:
I've created two slicers that are feeding off two disconnected tables; one containing month numbers, and one containing years. Based on the selection from each of these I have a measure that creates a month end using the EOMONTH(DATE(),0). This measure is called "Comparison_Month_End".
I then have another measure called "Compare_Orders" that contains the following:
CALCULATE([Total orders],FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]))
The point of all of this - this is meant to get the orders from the GSR where the month and year match the slicer selection. I want this value to appear in the pivot for every month selected i.e. not filtered by the pivot.
It doesn't work, however. This seems to me to be counter to what the book says, which is that if the filter in the measure is applied to a column on the pivot (in this case Month_End), the pivot filter is overriden. So, for example, if I have 31-Mar-18 in a column on my pivot and the comparison month end is 31-Jan-18, I would expect to see the January orders within "Compare_Orders" sitting next to the March orders, the February orders, the December orders and so on, but it only appears next to January.
To me this is doing exactly the same as an example in the book where there is a pivot with Year in the rows, Total Sales in column 1 and then 2002 sales in column 2. Column 1 shows sales for each year, column 2 shows the same sales figure (i.e. 2002 sales) against every row, even where the row year is 2001, 2003 or 2004. The 2002 Sales measure is using CALCULATE() summing Total Sales, filtering on "Sales[Year] = 2002".
Could anyone please explain why what I expected to happen doesn't seem to be happening please?
Thank you.
CALCULATE does indeed override the filter context when evaluating its first argument; using the rows provided to its second argument as the new filter context. However; CALCULATE does not alter the filter context when evaluating its second argument, and so pre-existing filters remain unless they are explicitly removed.
In FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]), FILTER only iterates over rows of GSR that are present in the pre-existing filter context (the pivot context). To get all rows, use ALL:
CALCULATE([Total orders],FILTER(ALL(GSR),GSR[Month_End]=[Comparison_Month_End]))