I have the below table and trying to move from excel to Power Bi. In excel I use =COUNTIFS($A$2:$A$16,"<="&E2,$B$2:$B$16,">="&E2) to get Count value but I wonder how can I calculate it in Power BI
open_date close_date Date Count
16-Sep-18 14-Jan-19 16-Sep-18 1
21-Sep-18 19-Jan-19 17-Sep-18 1
23-Sep-18 21-Jan-19 18-Sep-18 1
17-Jan-19 27-Jan-19 19-Sep-18 1
26-Jan-19 28-Jan-19 20-Sep-18 1
27-Jan-19 28-Jan-19 21-Sep-18 2
19-Jan-19 19-Jan-19 22-Sep-18 2
19-Jan-19 29-Jan-19 23-Sep-18 3
27-Jan-19 29-Jan-19 24-Sep-18 3
20-Jan-19 30-Jan-19 25-Sep-18 3
23-Jan-19 30-Jan-19 26-Sep-18 3
26-Jan-19 30-Jan-19 27-Sep-18 3
28-Jan-19 30-Jan-19 28-Sep-18 3
21-Jan-19 31-Jan-19 29-Sep-18 3
25-Jan-19 31-Jan-19 30-Sep-18 3
There are multiple ways to do this, but they'll all use some sort of filtering.
Here are a couple examples:
CountIf =
COUNTROWS (
FILTER (
ALL ( Table1 ),
Table1[open_date] <= MAX ( Table1[Date] ) &&
Table1[close_date] >= MAX ( Table1[Date] )
)
)
and
CountIf =
VAR CurrentDate =
MAX ( Table1[Date] )
RETURN
CALCULATE (
COUNT ( Table1[Date] ),
ALL ( Table1 ),
Table1[open_date] <= CurrentDate,
Table1[close_date] >= CurrentDate
)
Related
I am looking for a DAX measure to solve the following problem:
Count the number of rows in the dimension table where the Fact table either has no rows or the score is 0.
Table A (Dimension Table)
ID
name
1
a
2
b
3
c
Table B (Fact Table)
ID
score
1
0
1
1
1
2
2
5
Expected Result
In this example, I would expect 2, as ID=1 has one row with score=0 and ID=3 as no corresponding row in the Fact Table.
I came up with this measure which gives me the number of rows that have no corresponding row in the fact table, but I am not able to integrate the first condition:
CALCULATE(COUNTROWS('Dimension'), FILTER ('Dimension', ISBLANK ( CALCULATE ( COUNT ('Fact'[id]) ) )))
Probably much more straightforward methods, but try this measure for now:
MyMeasure =
VAR MyTable =
ADDCOLUMNS(
Table_A,
"Not in Table_B", NOT (
Table_A[ID]
IN DISTINCT( Table_B[ID] )
),
"Zero Score in Table_B",
CALCULATE(
COUNTROWS( Table_B ),
Table_B[score] = 0
) > 0
)
RETURN
SUMX(
MyTable,
[Not in Table_B] + [Zero Score in Table_B]
)
You can also try this
CountID =
VAR ScoreZero =
COUNTROWS ( FILTER ( TableB, [score] = 0 ) )
VAR NonExistentIDs =
COUNTROWS ( EXCEPT ( DISTINCT ( TableA[ID] ), DISTINCT ( TableB[ID] ) ) )
RETURN
ScoreZero + NonExistentIDs
This also works, not sure it's a good idea to nest CALCULATE:
CALCULATE(COUNTROWS('Table_A'), FILTER ('Table_A', ISBLANK ( CALCULATE ( COUNT ('Table_B '[id]) ) ) || CALCULATE(COUNTAX(Filter('Table_B ','Table_B '[score]=0),'Table_B '[id])>=1)))
I have a table with the following columns:
Industry table
Industry_ID Score
1 2
1 3
2 2
2 4
3 0
4 2
I need to calculate the average of each industry and then the average of those averages.
Like avg of scores of
1=(2+3)/2 =>2.5
2=(2+4)/2 =>3
3=0/1 => 0
4=2/1 => 2
Then average of these averages, i.e (2.5+3+0+2)/4 => 1.85
The tables are in direct query so please consider that. Any help is appreciated. Thank you
For creating the average of distinct values, create a calculated column as:
Average =
var no_ID = 'Table'[Industry_ID]
Return
AVERAGEX(
FILTER(ALL('Table'), 'Table'[Industry_ID] = no_ID),
'Table'[Score]
)
This will give you a column having average of distinct Industry_ID.
For creating an average of averages, create a measure as:
Measure = AVERAGEX(SUMMARIZE('Table', 'Table'[Industry_ID], 'Table'[Average]), 'Table'[Average])
Final Output-
Here are 2 ways to achieve that:
Just switch between Individual and Overall Average variables in the RETURN part, also store this code CALCULATE ( COUNTROWS ( Industry ) ) in a separate measure so that it can be re-used in various places without making the code verbose
Industry Average =
VAR AllIndustryAverages =
AVERAGEX (
ALL ( Industry[IndustryID] ),
DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
)
VAR IndividualAverages =
AVERAGEX (
VALUES ( Industry[IndustryID] ),
DIVIDE ( [Total Score], CALCULATE ( COUNTROWS ( Industry ) ) )
)
RETURN
IndividualAverages
Industry Average 2 =
VAR VisibleIndustries =
VALUES ( Industry[IndustryID] )
VAR AllIndustryAverages =
ADDCOLUMNS (
ALL ( Industry[IndustryID] ),
"Average",
VAR CurrentIndustryTotalScore = [Total Score]
VAR IndustryCount =
CALCULATE ( COUNTROWS ( Industry ) )
RETURN
DIVIDE ( CurrentIndustryTotalScore, IndustryCount )
)
VAR IndividualAverages =
AVERAGEX (
FILTER ( AllIndustryAverages, Industry[IndustryID] IN VisibleIndustries ),
[Average]
)
VAR OverallAverage =
AVERAGEX ( AllIndustryAverages, [Average] )
RETURN
IndividualAverages
I've a table like below in Power BI with only the start and end date with the value between this dates.
Start_date
End_date
Value
2020-12-01
2020-12-03
7
2020-12-04
2020-12-17
8
2020-12-18
2020-12-21
6
2020-12-22
2099-12-31
7
How could I show the value of a specific day (e.g. on 2020-12-20 the show value be 6) using a measure?
If your specific day is in DateTable then write for example:
Measure = calculate( max('Table'[Value]), FILTER(ALL('Table'),
'Table'[Start_date]<= SELECTEDVALUE(DateTable[Date])
&& SELECTEDVALUE(DateTable[Date]) <= 'Table'[End_date] )
)
Let's suppose you set the specific day using a slicer DimDate[Date].
Then you can write your measure like this:
Measure =
VAR DateSelected = SELECTEDVALUE ( DimDate[Date] )
RETURN
SUMX (
FILTER (
Table1,
Table1[Start_date] <= DateSelected &&
Table1[End_date] >= DateSelected
),
Table1[Value]
)
I have a table like this :
Id numbers_old numbers_new date
1 5 0 2019-02-13
1 8 3 2019-02-14
2 2 0 2019-02-13
2 12 10 2019-02-14
2 15 5 2019-02-15
I want a measure which will calculate volume_total = (number_old where date=min(date)) + (numbers_new where date != min(date))
How can I achieve it with a Power BI measure?
You can write the measure:
volume_total =
VAR DateMin =
MIN ( Table1[date] )
VAR SumOld =
CALCULATE (
SUM ( Table1[numbers_old] ),
Table1[date] = DateMin
)
VAR SumNew =
CALCULATE (
SUM ( Table1[numbers_new] ),
Table1[date] <> DateMin
)
RETURN
SumOld + SumNew
Worked example PBIX file: https://pwrbi.com/so_54707672/
Problem:
I need a calculated measure in DAX that sums the Value column for the last 6 sprints. I am basing the last 6 sprints on the DimSprintEndDateKey in descending order.
Table structure in PowerBI
The DAX that I am using:
CALCULATE (
SUM ( factSprint[Value] ),
FILTER (
ALL ( factSprint ),
COUNTROWS (
topn(6,
FILTER (
factSprint,
EARLIEST( RELATED ( dimSprint[DimSprintEndDateKey] ) )
> RELATED ( dimSprint[DimSprintEndDateKey] )
),RELATED ( dimSprint[DimSprintEndDateKey] ), DESC
)
)
)
)
I am assuming that the relationship on your tables is between 'dimSprint'[dimSprintKey] and 'FactSprint'[dimSprintKey].
That being the case, this measure could work for you.
Total Value Last Six Sprints =
VAR endDateSprint =
LOOKUPVALUE (
'dimSprint'[dimSprintEndDateKey],
'dimSprint'[dimSprintKey], SELECTEDVALUE ( 'FactSprint'[dimSprintKey] )
)
VAR dimTableFiltered =
FILTER ( 'dimSprint', 'dimSprint'[dimSprintEndDateKey] <= endDateSprint )
RETURN
CALCULATE (
SUM ( 'FactSprint'[Value] ),
ALL ( 'FactSprint' ),
TOPN ( 6, dimTableFiltered, [dimSprintEndDateKey], DESC )
)
Use it on a matrix visual (or pivottable). Be sure to put 'FactSprint'[dimSprintKey] or 'FactSprint'[SprintPK] on Rows and [Total Value Last Six Sprints] on Values.