Create a new column and have it display in number values - powerbi

I have 1 file which has the following columns: Document Date and Disposition Date.
In Power BI Desktop, I'd like to create another column called 'Duration' which can be calculated by taking Disposition Date - Document Date and I want the new column to display in number values since both the Disposition Date & Document Date are either in serial number (ex: 39448) or date (ex: 09/25/2018) format.
Is there a code or something to do this? Thank you!

I may be missing the point here, but if you have a dataset such as this:
Document Disposition
25.09.2018 22.09.2018
24.09.2018 21.09.2018
23.09.2018 20.09.2018
22.09.2018 19.09.2018
21.09.2018 18.09.2018
20.09.2018 17.09.2018
19.09.2018 16.09.2018
18.09.2018 14.09.2018
17.09.2018 13.09.2018
16.09.2018 12.09.2018
15.09.2018 11.09.2018
14.09.2018 10.09.2018
13.09.2018 09.09.2018
12.09.2018 08.09.2018
11.09.2018 07.09.2018
10.09.2018 06.09.2018
09.09.2018 05.09.2018
08.09.2018 04.09.2018
Then you can load them using Get Data, go to Edit Queries, select Add Column, and simply set it up like this:
Then you can click the ABC / 123 icon on top of the column and change the datatype to Whole number, and you'll get this:
Please let me know if this is not what you were looking for.

First create two new date columns for document and disposition since there are some variances in datatype. I am basically just checking if after conversion, there is a "/" in the date field implying it is a date type, if not I am assuming it is serialized and will convert. The following DAX should do it BUT it is not tested, so try it out.
True Document Date :=
SWITCH (
TRUE (),
AND (
ISERROR ( SEARCH ( "/", FORMAT ( [Document], "text" ) ) ),
[Document] >= 32767
), FORMAT ( DATE ( 2000, 1, [Document] - 36523 ), "YYYY-MM-DD" ),
AND (
ISERROR ( SEARCH ( "/", FORMAT ( [Document], "text" ) ) ),
[Document] < 32767
), FORMAT ( DATE ( 1900, 1, Sheet1[DATE SERIAL NUMBER] ), "YYYY-MM-DD" ),
NOT ( ISERROR ( SEARCH ( "/", FORMAT ( [Document], "text" ) ) ) ), [Document]
)
True Disposition Date :=
SWITCH (
TRUE (),
AND (
ISERROR ( SEARCH ( "/", FORMAT ( [Disposition], "text" ) ) ),
[Disposition] >= 32767
), FORMAT ( DATE ( 2000, 1, [Disposition] - 36523 ), "YYYY-MM-DD" ),
AND (
ISERROR ( SEARCH ( "/", FORMAT ( [Disposition], "text" ) ) ),
[Disposition] < 32767
), FORMAT ( DATE ( 1900, 1, Sheet1[DATE SERIAL NUMBER] ), "YYYY-MM-DD" ),
NOT ( ISERROR ( SEARCH ( "/", FORMAT ( [Disposition], "text" ) ) ) ), [Disposition]
)
Then, just take the difference in days and store results a new calculated column:
Date Delta :=
DATEDIFF ( [True Document Date], [True Disposition Date], DAY )

Related

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

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

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

Convert comma separated text to a list of numbers

In Power BI, I have created a DAX query created with a var giving comma-separated text using CONCATENATEX function.
Output like
Var = "1,2,3,4,5,6"
Now I want to search this var into my table column with syntax
Table[col] in {var}.
It is throwing an error.
I even tried converting the column to string with syntax
Convert(table[col], string) in {var}
The error is removed but the data doesn't match with column data.
I found this Power BI community thread that is essentially the same question.
The solution is to convert the string into a path object.
Here's the code from the link:
Measure 3 =
VAR mymeasure =
SUBSTITUTE ( [Measure], ",", "|" )
VAR Mylen =
PATHLENGTH ( mymeasure )
VAR mytable =
ADDCOLUMNS (
GENERATESERIES ( 1, mylen ),
"mylist", VALUE ( PATHITEM ( mymeasure, [Value] ) )
)
VAR mylist =
SELECTCOLUMNS ( mytable, "list", [mylist] )
RETURN
CALCULATE ( COUNTROWS ( Table1 ), Table1[ID] IN mylist )
There's a bit of redundancy in the above, so I'd probably condense it a bit and write it like this:
SomeMeasureFilteredByVar =
VAR PathVar = SUBSTITUTE ( [Var], ",", "|" )
VAR VarList =
SELECTCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( PathVar ) ),
"Item", VALUE ( PATHITEM ( PathVar, [Value] ) )
)
RETURN
CALCULATE ( [SomeMeasure], 'Table'[ID] IN VarList )
Edit: To just create a calculated table, you can skip the last step:
TableFromString =
VAR PathVar = SUBSTITUTE ( "1,2,3,4,5,6", ",", "|" )
RETURN
SELECTCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( PathVar ) ),
"Item", VALUE ( PATHITEM ( PathVar, [Value] ) )
)
Note that it is not possible to create a calculated table that is dynamically responsive to report filters and slicers. Materialized calculated tables are only calculated once when the data loads, not every time you adjust a filter or slicer.
Therefore, you can use a measure in the table instead of the string "1,2,3,4,5,6" but the table output will be the same regardless of what the measure returns within different filter contexts.

DAX Convert date to the first day of quarter

While building a calendar table I came across unexpected hard nut to crack.
How to convert date to the first date of the quarter? Here are some examples:
2019-02-01 > 2019-01-01
2019-03-31 > 2019-01-01
2019-04-02 > 2019-04-01
The function STARTOFQUARTER does not work in my calendar. I do not know why.
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016, 1, 1 ),
DATE ( 2020, 12, 31 )
),
VAR VarDates = [Date]
var YQ_date = STARTOFQUARTER( [Date] ) -- this does not work
RETURN
ROW (
"day" , VarDay,
"YQ_date" , YQ_date
)
)
The only option seems to be adding calculated column to Calendar table. But if it is possible to have it straight, then why not have it straight?
How about as an added column?
Calendar =
ADDCOLUMNS (
CALENDAR ( DATE ( 2016, 1, 1 ), DATE ( 2020, 12, 31 ) ),
"YQ_date", EOMONTH ( [Date], -1 - MOD ( MONTH ( [Date] ) - 1, 3 ) ) + 1
)
For the STARTOFQUARTER function to work, you need the dates that you expect to be returned by the function in your datetable.
So in the provided sample, you need to add the dates 2019-01-01 and 2019-04-01.
Something like this:
When you want to build a calendar table with one DAX expression, you cannot use the STARTOFQUARTER function, because it will not work with an in-memory table.
You could use something like this as a workaround though:
Calendar =
GENERATE (
CALENDAR (
DATE ( 2016; 1; 1 );
DATE ( 2020; 12; 31 )
);
ROW (
"YQ_date"; DATE( Year( [Date] ); ROUNDUP( MONTH ( [Date] ) / 3; 0 ) * 3 - 2; 1)
)
)

SSAS Tabular DAX using of IF Statement

I have this query written in SQL:
SELECT
CustomerId,
CustomerType,
CASE WHEN CustomerStatus = 'VIP'
THEN CustomerDiscountType
ELSE NULL
END AS CustomerDiscountType
FROM Customer
And I would like to write this in DAX query:
I know that I can write like this:
EVALUATE
(
SUMMARIZE
(
'Customer',
'Customer'[CustomerId],
'Customer'[Type],
'Customer'[DiscountType],
"Customer VIP", IF('Customer'[Status] = "VIP", 'Customer'[DiscountType], BLANK())
)
)
But when I write if condition I need include attribute 'Customer'[DiscountType] in that query too, but I would like to write the column name of that IF statement
"DiscountType", but it isn't possible for me like below.
EVALUATE
(
SUMMARIZE
(
'Customer',
'Customer'[CustomerId],
'Customer'[Type],
"DiscountType", IF('Customer'[Status] = "VIP", 'Customer'[DiscountType], BLANK())
)
)
It failed with this error because of existing DiscountType column:
Function 'SUMMARIZE' cannot add column [DiscountType] since it already exists.
Instead of using SUMMARIZE you can use SELECTCOLUMNS function. I think the error is caused because the IF function returns DiscountType column and it already exists in your Customer table. Also I am unsure if SUMMARIZE works without any aggregation like used in your DAX expression.
Try this expression:
EVALUATE
(
SELECTCOLUMNS (
'Customer',
"CustomerId", 'Customer'[CustomerId],
"Type", 'Customer'[Type],
"DiscountType", IF ( 'Customer'[Status] = "VIP", 'Customer'[DiscountType], BLANK () )
)
)
UPDATE: OP tells via comments the version used is SSAS 2014 which doesn't support SELECTCOLUMNS function.
You can use a mix of SUMMARIZE and ADDCOLUMNS functions to get the expected result.
EVALUATE
(
SUMMARIZE (
ADDCOLUMNS (
ADDCOLUMNS (
DISTINCT ( Customer[CustomerID] ),
"Type", CALCULATE ( VALUES ( Customer[Type] ) ),
"Status", CALCULATE ( VALUES ( Customer[Status] ) ),
"DiscountType1", CALCULATE ( VALUES ( Customer[DiscountType] ) )
),
"DiscountType", IF ( [Status] = "VIP", [DiscountType1], BLANK () )
),
[CustomerId],
[Type],
[DiscountType]
)
)
It is not tested but should work, let me know if this helps for you.