Count Occurances of value in 2 columns - powerbi

I have a simple job to do in PowerBi but for some reason I can not get my head around it.
I have projects table - in this table we have "date of start" and "date of end". What I am after is a smaller table with "Year & Quarter" first column and a count of projects started and count of project ended in that "Year & Quarter".
Project Number
Started
Ended
xxxx23
2019-01
2019-03
xxxx24
2019-03
2020-01
xxxx25
2019-03
2020-02
what I am after is something like below:
Year & Quarter
Project Started
Project Ended
2019-01
1
0
2019-03
2
1
2019-04
0
0
When doing the calculations, I am getting wrong counts. Any help is really appreciated!

You can use UNION to create a new table. You can use the original table twice but with different columns.
NewTable =
UNION(
SELECTCOLUMNS( Projects,
"date",Projects[started],
"project",Projects[project],
"Started",1,
"Ended",0
)
,
SELECTCOLUMNS( Projects,
"date",Projects[ended],
"project",Projects[project],
"Started",0,
"Ended",1
)
)

If you have a Calendar Table, you can achieve the ask by doing following two ways
#1
newTable =
VAR _1 =
SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] )
VAR _2 =
ADDCOLUMNS (
ADDCOLUMNS (
_1,
"Project Started",
COUNTX (
FILTER ( Projects, Projects[started] = EARLIER ( [Yr & Qt] ) ),
[started]
)
),
"Project Ended", COUNTX ( FILTER ( Projects, Projects[ended] = EARLIER ( [Yr & Qt] ) ), [ended] )
)
RETURN
_2
#2
Table 2 =
ADDCOLUMNS (
ADDCOLUMNS (
SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ),
"Project Start Count",
CALCULATE (
CALCULATE (
COUNTX ( Projects, Projects[started] ),
TREATAS ( SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ), Projects[started] )
)
)
),
"Project End Count",
CALCULATE (
CALCULATE (
COUNTX ( Projects, Projects[ended] ),
TREATAS ( SUMMARIZE ( 'Calendar', 'Calendar'[Yr & Qt] ), Projects[ended] )
)
)
)
results in following
The solution has minimum dependency to a Calendar tbl with a column like Yr & Qt as following

Related

Filter summarizecolumns table within measure using slicer - DAX for Power Pivot

I have a table my_data with the following columns: CATEGORY, SUPPLIER and AMOUNT. I have created measures to calculate the total amount:
Total_Amount := SUM(my_data[AMOUNT])
and to do an ABC classification, I created a ranking:
Ranking:=IF (
ISBLANK ( [Total_Amount] ),
BLANK (),
RANKX (
FILTER (
ALL ( my_data[CATEGORY], my_data[SUPPLIER] ),
my_data[CATEGORY] = MAX ( my_data[CATEGORY] )
),
CALCULATE ( [Total_Amount] )
)
)
a Running Total:
Running_Total:=VAR current_rank = [Ranking]
RETURN
SUMX (
FILTER (
ALL ( my_data[SUPPLIER] ),
[Ranking] <= current_rank
),
[Total_Amount]
)
a Running Total %:
Running_Total(%):=DIVIDE (
[Running_Total],
SUMX ( ALL ( my_data[SUPPLIER] ), [Total_Amount] ),
BLANK ()
)
and the ABC classifier:
ABC_class:=IF (
ISBLANK ( [Total_Amount] ),
BLANK (),
SWITCH (
TRUE (),
[Running_Total(%)] <= [Class_A], "A",
[Running_Total(%)] <= [Class_B], "B",
"C"
)
)
Now, my problem. I have several slicers. Once of them is to choose A, B and/or C from the ABC classification. Note, that in my data there is no column with A, B or C data. The classification is only a measure. So I used a trick to be able to connect the slicer to my Pivot Table and that works fine. PROBLEM: I do not manage to get the right subtotals and grand totals for the following measure:
measure:=VAR Selected_Class =
ALLSELECTED ( ABC_table[Class] )
VAR Supplier_Class =
CALCULATE (
[ABC_class],
ALLEXCEPT (
my_data,
my_data[CATEGORY],
my_data[SUPPLIER],
Period_Table[YEAR]
)
)
RETURN
IF (
HASONEFILTER ( my_data[SUPPLIER] ),
IF (
CONTAINSROW ( Selected_Class, Supplier_Class ),
[Total_Amount],
BLANK ()
),
SUMX (
FILTER (
SUMMARIZECOLUMNS (
StockMvts[SUPPLIER],
"total", [Total_Purchased(EUR)],
"class", [ABC_TotPurchased_byCat&Sup]
),
CONTAINSROW(Selected_Class, [class])
),
[total]
)
)
This last measure doesn't work. It gives an error. The problem is in the SUMX for the subtotals and grand totals. How do I make it to sum only the values of [Total_Amount] for the [SUPPLIER] where the [ABC_class] measure results in one of the values selected in the ABC slicer?
Note, I'm using Power Pivot on Excel.
Thank you!

Countrows/calculate/sum rows in DAX

I am fairly new with Power BI and DAX and I'm stuck. I'll try to explain the current situation and what I want to become my output. I've tried a lot of meaures with distinctcount, calculate, you name it, I did it. But can't find the right solution.
We've got 4 columns: Date, Employee_ID, Sick, %FTE. Every row records if an employee was sick on that date. Blank is not sick and Y = sick.
I would like to create a measure where it counts the %FTE just once when an employee is sick in a particular week, month or year.
So the output of January should be 2,13 (0,8 + 0,33 + 1) and in February 1,8 (0,8 + 1).
enter image description here
You would need two additional columns in the dataset as following
Once you have that, you can use the following measures to reach the goal
Measure8 =
VAR _1 =
IF (
MAX ( 'fact'[sick] ) <> BLANK (),
RANKX (
FILTER (
ALL ( 'fact' ),
'fact'[emp_id] = MAX ( 'fact'[emp_id] )
&& 'fact'[Year] = MAX ( 'fact'[Year] )
&& 'fact'[Month] = MAX ( 'fact'[Month] )
&& 'fact'[sick] = "Y"
),
CALCULATE ( MAX ( 'fact'[date] ) ),
,
ASC,
DENSE
)
)
VAR _2 =
IF ( _1 = 1, IF ( MAX ( 'fact'[sick] ) = "y", MAX ( 'fact'[%FTE] ) ) )
RETURN
_2
Measure9 =
IF (
HASONEVALUE ( 'date'[date] ),
[Measure8],
VAR _1 =
MAXX (
GROUPBY (
ADDCOLUMNS ( 'fact', "val", [Measure8] ),
[Year],
[Month],
"total", SUMX ( CURRENTGROUP (), [val] )
),
[total]
)
VAR _2 =
MAXX (
GROUPBY (
ADDCOLUMNS ( 'fact', "val", [Measure8] ),
[Year],
"total", SUMX ( CURRENTGROUP (), [val] )
),
[total]
)
VAR _3 =
IF ( ISINSCOPE ( 'fact'[Year] ), _2, _1 )
RETURN
_3
)
Also, for any future posts please provide the sample data and expected output as markdown tables How To

DAX subquery measure?

I'm struggling with a DAX query and wondered if you could help?
Consider this table (or visualisation) called 'Builds':
Build....App....Status
Build1...App1...UAT
Build1...App2...Complete
Build2...App1...Complete
Build2...App2...Complete
I would like to add a measure column called 'AppsOutstanding' to show a count of Apps for that Build that aren't 'Complete'. Like so:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....1
Build2...App1...Complete....0
Build2...App2...Complete....0
I almost need to do a 'subquery' measure!? Something like:
SELECT COUNT(Status) FROM Builds
WHERE Build = [The Build In This Row]
AND Status <> 'Complete'
I'm a bit stumped how to translate this into DAX? Here is my unsuccessful attempt:
AppsUnavailable = CALCULATE (
count(Builds[Build]),
CALCULATETABLE (
SUMMARIZE ( Builds,Builds[Status] ),
Builds[Status] <> "Complete"
))
Thanks in advance!
UPDATE
I've tried this, but the count isn't working, and also this DAX filters "Complete" statuses out of my actual results! Which i don't want. I only want to filter the "Complete" statuses out of my count measure....
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (
ALL ( Builds[Status] ),
Builds[Status] <> "Complete"
)
)
UPDATE 2
I think I'm doing something fundamentally wrong. I've really dumbed it down to just find other 'Builds' with the same name, and it still only returns 1's!
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
)
)
UPDATE 3
This query (when testing on a single table (no joins) sample) produces this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....0
Build1...App2...UAT.........1
Build2...App1...Complete....0
Build2...App2...Complete....0
But i actually need this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........2
Build1...App2...Complete....0
Build1...App2...UAT.........2
Build2...App1...Complete....0
Build2...App2...Complete....0
So Build1 has 2 Apps that are not complete.
I then need to look into why I'm getting all 1's in the 'live' environment. It must be to do with filtering on 2 tables as opposed to 1....
You can replace [The Build In This Row] with SELECTEDVALUE ( Builds[Build] ).
Try this:
AppsUnavailable =
CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Status], Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
&& Builds[Status] <> "Complete"
)
) + 0
powerBI Table visualization internal use SUMMARIZECOLUMNS which is removed row without data from the measure. You have 2 options:
-Right click on GroupBy Column -> "Show item with no data"
-Add +0 to calculation after the last parenthesis
UPDATE:
CALCULATE (
COUNT ( YourTable[App] ),
FILTER (
ALL ( YourTable[App], YourTable[Build], YourTable[Status] ),
YourTable[Build] = SELECTEDVALUE ( YourTable[Build] )
&& YourTable[Status] <> "Complete"
)
) + 0

How can I create a new table in Power BI from three columns of another table with a filter?

I have a table with repeating values ​​in the index column, and a third column that has values ​​only in some rows
Table I have now:
My objective is to create a new table that selects the CompanyID, Start Date, and Contact Date columns but excludes the rows that have blank values from any of those
New table I want to have:
I used the following DAX code:
CALCULATETABLE (
Table,
FILTER ( Table, NOT ( ISBLANK ( Table[Start Date] ) ) )
)
The issue with this code is that it selects all 350+ columns of my original data instead of just having the 3 columns I want. Additionally, I couldn't figure out how to add additional filters so I don't have blank rows for Contact Date and ComapanyID
I did try the below code as well, but it doesn't work
CALCULATETABLE (
Table,
FILTER ( Table, NOT ( ISBLANK ( Table[Start Date] ) ) ),
FILTER ( Table, NOT ( ISBLANK ( Table[Order Date] ) ) ),
FILTER ( Table, NOT ( ISBLANK ( Table[Employee Count] ) ) )
)
The SELECTCOLUMNS function allows you to pick specific columns.
For example:
New Filtered Table =
EVALUATE
SELECTCOLUMNS (
CALCULATETABLE (
Table,
NOT ( ISBLANK ( Table[Start Date] ) ),
NOT ( ISBLANK ( Table[Order Date] ) ),
NOT ( ISBLANK ( Table[Employee Count] ) )
),
"CompanyID", Table[CompanyID],
"Contact Date", Table[Contact Date],
"Start Date", Table[Start Date]
)

How to make running total ignore other columns DAX

I have a table in Power BI that calculates the running total. However, when I add additional columns to the table, the values get messed up. Any ideas on how get the running total to ignore specific columns?
Here is my code:
Measure =
CALCULATE (
SUM ( Append1[AVAILABLE] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <> MAX ( Append1[DUE DATE] )
)
)
+ CALCULATE (
SUM ( Append1[ORDER QTY] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <= MAX ( Append1[DUE DATE] )
)
)
- CALCULATE (
SUM ( Append1[REQUIREMENT QTY] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <= MAX ( Append1[DUE DATE] )
)
)
Below are pictures of what the table looks like when it runs correctly and what it looks like when I add another column and the values get messed up.
Correct running total:
Incorrect Running Total:
Thanks in advance for your help!
I was able to get it working correctly using ALLSELECTED. Thanks for your help.