Tableau - multiple records that meet IF condition - if-statement

I would like to multiple values that meet criteria in tableau.
For example:
if [order status] = cancelled and [dtime] = express then 39
else other end (so it counts number of records *39)
What I need is to not count the number of records but all unique order_id’s so I used:
if [order status] = cancelled and [dtime] = express then (countd[order id] * 39)
but this does not work.
scenario :
COUNTD(If
[Expedition Type] = "ZEX" and [count bigboxy aj smallboxy] = "BIGBOX" and [Transporter] = "Mall Box" then [Order Id] * 260.6
ELSEIF ([Expedition Type] = "ZEX" and [count bigboxy aj smallboxy] = "BIGBOX" and [Transporter]= "Mall Pick-Up") then [Order Id] * 260.7
ELSEIF ([Expedition Type] = "ZEX" and [count bigboxy aj smallboxy] = "BIGBOX" and [Transporter] = "Ulozenka") then [Order Id] * 260.6
ELSEIF ([Expedition Type] = "ZEX" and [count bigboxy aj smallboxy] = "BIGBOX" and [Transporter] = "--empty--" or [Transporter] = "Cash and Carry" or [Transporter] = "Česká pošta"
or [Transporter] = "DPD" or [Transporter] = "Helicar" or [Transporter] = "Mall doprava" or [Transporter] = "PPL CZ" or [Transporter] = "Speedy Kuryr" or [Transporter] = "TopTrans CZ" or [Transporter] = "WE|DO"
or [Transporter] = "Gebrüder Weiss") then [Order Id] * 260.6 END)

COUNTD(if [order status] = cancelled and [dtime] = express then [order id] END) * 39

Related

Compare Selected quarter to last quarter in DAX, even when i select Q1 it should compare with last year's Q4

Hi i am working on a solution in DAX where i have to find customers not ordered in current Quarter compared to its last quarter, i am able to compare Q2, Q3 and Q4 but when i select Q1 i get no value. if i select it should compare with Q4 of last year
how this can be achieved?
Thanks
this was my original question
get new customers compared to last month in dax power bi
with a little bit of tinkering i am here now
Customers Not ordered This Quarter =
VAR ThisQuarter =
SELECTEDVALUE( DailyReport[DateCreated].[QuarterNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastQuarter = ThisQuarter - 1
VAR CustomersThisQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = ThisQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = LastQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersLastQuarter, CustomersThisQuarter )
RETURN
CONCATENATEX( T1, [C1], UNICHAR(10), [C1], ASC)
it should compare with the last year's Q4 when i select Q1 in current year
You really need a date table. I have written the code below blind but it should work.
Customers Not ordered This Quarter =
VAR ThisQuarter =
SELECTEDVALUE( DailyReport[DateCreated].[QuarterNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastQuarter = IF(ThisQuarter = 1,4,ThisQuarter - 1)
VAR LastYear = IF(ThisQuarter = 1,ThisYEAR -1,ThisYEAR)
VAR CustomersThisQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = ThisQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = ThisYEAR),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastQuarter =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[QuarterNo] = LastQuarter && DailyReport[SupplierName] = SelectedSupplier && DailyReport[DateCreated].[Year] = LastYEAR),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersLastQuarter, CustomersThisQuarter )
RETURN
CONCATENATEX( T1, [C1], UNICHAR(10), [C1], ASC)

Trying to get list of Venues with respect to Suppliers

Hi guys so i am trying to build a report where i have to show new customers this month compared to last month. I am able to calculate new customers fine but i have to show them in a Matrix in Power Bi. This is the Code i am using
New Customers This Month =
VAR ThisMonth =
SELECTEDVALUE( DailyReport[DateCreated].[MonthNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastMonth = ThisMonth - 1
VAR CustomersThisMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = ThisMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = LastMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersThisMonth, CustomersLastMonth )
RETURN
CONCATENATEX( T1, [C1], ", " )
I am getting result like this, two columns one for supplier and one for new venues this month
getting result like this
But expected result should be like this, you know like a list
expected result
i think it may be causing due to how i am concatening it in the last line in code
and please let me know if you need any more information

Count of active rows between two dates, sliced by department

I'm trying to get count of active employees by date that can be filtered by department.
The following is what my Employees data more or less looks like. Additionally, there's a dim_Department table connected to the main Employees table and a TERMINATED(Y/N) column. I found a similar case here (without the departments and terminated(y/n) though):
I used the following DAX expression to get a table of total number of employees (organization-wide), but I'm not sure how to filter this metric using the slicer showing departments.
CountOfActive =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('employee'), filter(ALL(employee), employee[Hire Date] <= VALUE(_selectedDate) && (employee[Termination Date] >= VALUE(_selectedDate) || ISBLANK(employee[Termination Date]))))
CountOfTerminated =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('employee'), filter(ALL(employee), employee[Hire Date] <= VALUE(_selectedDate) && (employee[Termination Date] < VALUE(_selectedDate) )))
How do I get my department slicer to filter the results of this column? Or should I use a different expression, or make a column/table for each department?
we have answered that before i guess...
Unique Count
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
Unique Count (Active)
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)
Unique Count (Terminated)
Unique Count (Terminated) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Terminated"
)
also you can insert your data into that sample file and see if it works...

Count unique occurrences within a year

My database schema looks like down below
ID
Date
Status
ID1
2022/01/01
Active
ID1
2022/02/01
Active
ID1
2022/03/01
Active
ID1
2022/04/01
Terminated
ID2
2022/01/01
Active
ID2
2022/02/01
Terminated
I'd like to calculate unique occurrences from start of selected date year, till the selected date. My formula is:
CountOfUnique = CALCULATE( DISTINCTCOUNT( 'Table'[ID] ) , 'Table'[STATUS] = "Active", DATESBETWEEN('CALENDAR'[DATE], STARTOFYEAR('CALENDAR'[DATE]), MAX('CALENDAR'[DATE]) ))
In SQL I'd need something like
SELECT COUNT ( DISTINCT ID) FROM Table
WHERE STATUS = "ACTIVE"
AND DATE BETWEEN 2022/01/01 AND 2022/04/01
Try this:
CountOfUnique =
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID] ),
'Table'[STATUS] = "Active",
DATESBETWEEN (
'CALENDAR'[DATE],
STARTOFYEAR ( 'CALENDAR'[DATE] ),
SELECTEDVALUE ( 'CALENDAR'[DATE] )
)
)
when you have a slicer on the visual, the start of selected date year doesnt mean much as you select the dates on the slicer. I created a Calendar Table = CALENDARAUTO() so it started from the 2022/01/01...
use one of these as you like...
sample PBix File
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
or only Active if you need
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)

filter the new table by range of data, e.g. 1997-2020 PowerBI

Error:The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
I intend to select Data by datecolumns from 1997 to 2020. But no success
New Periode =
VAR DateStart =
DATE ( "1997", "1", "1" )
VAR DateEnd =
DATE ( "2021", "11", "10" )
RETURN
CALCULATETABLE (
'Date_data',
FILTER ( 'Date_data', 'Date_data'[Date] <= DateEnd && 'Date_data'[Date] >= DateStart )
)
Can you please try this below code-
Periode =
VAR DateStart = DATE ( "1997", "1", "1" )
VAR DateEnd = DATE ( "2021", "11", "10" )
RETURN
CALCULATETABLE (
'Date_data',
'Date_data'[Date] <= DateEnd
&& 'Date_data'[Date] >= DateStart
)