Power BI if condition if true then column with date value else NULL - powerbi

The below table has 2 columns
Where Column A is a Date column and Column B is a Text column where some values are equal to "x" and some are blank.
I need to create an output column which based on the below formula
IF (
AND ( ColumnA < EOMONTH ( ColumnA, 3 ), ( ColumnB = "x" ) ),
EOMONTH ( ColumnA, 3 ),
"-"
)
I have written the following DAX formula for it:
Output =
IF (
AND (
ColumnA
< EOMONTH ( DATE ( YEAR ( ColumnA ), MONTH ( ColumnA ), DAY ( ColumnA ) ), 3 ),
( ColumnB = "x" )
),
EOMONTH ( ColumnA, 3 ),
"-"
)
I'm getting an error with this formula that NULL is not allowed in this context
Note: We can leave Blank in place of "x".
How do I write the correct DAX formula to achieve the above?

The problem with your calculation is that you are mixing different data types in the same column.
The Output column is handling a date data types with a text data types, that's why you are getting an error. The columns could only handle date or text but not both at the same time.
To fix your calculation your need to change your ELSE statement from "-" to BLANK()

Related

DAX Measure to count Rows if relation may not exist

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)))

ALLEXCEPT not working when filtering blanks

I have a simple problem. My DAX measure does not seem to be working correctly when I filter for non-existing values. Here are some details:
Table:
Column1: A,A,A,A,A,B,B,B,B
Column2: 1,2,3,4,5,1,2,3,5
Measure = calculate(countrows(table), allexcept(column1))
Card Visual returns correct row count when I filter by column1 (any value in filtering pane)
However it returns wrong row count when I filter by column2 = "4" and Column1 = "B" (in filtering pane). It seems that it should ingore filtering by column2 and it does except when I specifically filer for value = "4". It gives "blank" result value in a card visual then.
Any ideas why?
Here's the screen. I would like to populate that blank cell with "4" (in a singe-table data model.enter image description here
In your case you dont need to add allexcept in your measure. Below code would be fine.
TestMeasure = countrows(Test_Data)
PFB screenshot
I am hoping that you have a data model as following
table name _dim1
colA
A
B
C
table name _dim2
colB
1
2
3
4
5
table name _fact
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
If you have this you can reach where you need by using following measures
Measure3 =
CALCULATE ( COUNTROWS ( _fact ), ALL ( _dim2[colB] ), VALUES ( _fact[colA] ) )
Measure9 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] <= _1 ),
LASTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
_2
Measure10 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] > _1 ),
FIRSTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
IF ( ISBLANK ( [Measure9] ) = TRUE (), _2, [Measure9] )
I don't think you can reach here from a single table like following
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3

Row calculated from existing rows given some condition

I'm really new to Power BI, I'm a data analyst and usually work with code, so I'm not really familiar with Excel like formulas
I have a table with like 20 columns, and I need only 2 columns for this calculation, so I'm going to zoom in the columns I need:
I want to add rows with Attribute5 for which isĀ  Attribute1 - Attribute2 - Attribute3.
The result should look like this :
I'm really stuck with this since yesterday morning
I achieved it with a Measure by:
Creating a table containing distinct attributes :
TableXX = UNION(DISTINCT('Table'[Attributes]),{{"Attribute5"}})
Doing the calculating with a measure :
Measure =
SUMX (
DISTINCT ( 'Table 3'[Attributes] ),
SWITCH (
'Table 3'[Attributes],
"Attribute5", CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute1" )
- CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute2" )
- CALCULATE ( SUM ( 'Table'[Value] ), 'Table'[Attributes] = "Attribute3" ),
var a = 'Table 3'[Attributes] return
CALCULATE ( SUM ( 'Table'[Value] ),'Table'[Attributes]=a)
)
)
But I don't really want a measure, I want to replace Value Column by this measure, how can I achieve that?
You can define a new table as the union of the existing table and a calculated table that has the new rows that you want:
NewTable =
VAR Table5 =
SUMMARIZE (
FILTER (
'Table 3',
'Table 3'[Attributes] IN { "Attribute1", "Attribute2", "Attribute3" }
),
'Table 3'[Date],
"Attributes", "Attribute5",
"Value", SUMX (
'Table 3',
IF (
'Table 3'[Attributes] = "Attribute1",
'Table 3'[Value],
- 'Table 3'[Value]
)
)
)
RETURN
UNION ( 'Table 3', Table5 )
The new calculated table is stored in the variable Table5 which takes only the rows containing attributes 1, 2, or 3 and for each date takes the sum of the values where attributes other than 1 get a negative sign.
I would say that the easiest way is to pivot Values column, so you will get a table with columns Date, Attribute1, Attribute2, Attribute3 and Attribute4:
Then add a custom column Attribute5:
And then unpivot attributes columns to get the table back:

Is there Dax code to take the difference between 2 rows from a summarized table

I need to be able to get the difference between 2 successive rows in a Summarized table, by rank, so that I can then get the average of the differences of each row. And I can't create a new table as I need this DAX query to be filterable
I've been able to get this far, but do not know how to add a difference column that will show the DSOValue difference between rows 1-2, 2-3, 3-4 ...
ADDCOLUMNS (
SUMMARIZE (
Table1,
Table1[Date],
"DSOValue", DIVIDE ( SUM ( 'Table1'[AR] ) * 91.5, SUM ( 'Table1'[Sales] ), 0 )
),
"Rank", RANKX (
Table1,
CALCULATE (
COUNTROWS ( Table1),
FILTER ( Table1, Table1[Date] <= EARLIER ( Table1[Date] ) )
),,ASC,DENSE)
)
I've tried embedding this code within another ADDCOLUMNS function, but it won't let me CALCULATE and FILTER on my created columns (DSOValue and RANK)
you can use the following:
Diff = 'Table1'[ DSOValue ] - LOOKUPVALUE('Table1'[ DSOValue ]; 'Table1'[Date ];CALCULATE( MAX('Table1'[Date ]);FILTER('Table1';'Table1'[Date ]<EARLIER('Table1'[Date ]))))
Note: You cannot use <= here, it will pick its own date and all will be null. It would also be easier to add an index column, when you have this you can use Lookupvalue with Index -1

Filter a column dynamically using a measure in Power BI

There is a column with FY i.e. "FY19","FY18","FY17" etc.
I Created a measure which calculates the current FY:
CurrFY =
CONCATENATE (
"FY",
IF (
MONTH ( TODAY () ) <= 3,
VALUE ( FORMAT ( TODAY (), "YY" ) ),
VALUE ( FORMAT ( TODAY (), "YY" ) ) + 1
)
)
e.g. output: "FY19"
Now i need to filter the report based on the FY column using the current FY i get from the CurrFY measure.
How do i do it?
Create a calculated column on your table using that measure
FYFilter = IF(Table1[FY] = [CurrFY], 1, 0)
Then add that column as a report level filter where FYFilter is 1.