MDX Calculated Measure for Customer and Route by Day - sql-server-2008-r2

I'm working with a cube that needs a new calculated measure and I'm having a difficult time getting the MDX right.
The scenario that I'm looking to measure is a count of days where for a given Customer (by Customer Name) and Delivery Route (also on the Customer Dimension) the total sales amount is greater than 0.
The date hierarchy is fairly straightforward.
Date > Year > Month > Day
In the customer dimension, we are not going to be using a hierarchy.
customer name
delivery route
On the measures, we only really care about total sales
total sales
So, I've tried a little bit of everything, but I'm not sure I'm approaching the problem in the right way and that could be that I'm less familiar with MDX.
Here's a sample of data and what my end goal would be.
What I've tried to get there has been the following (however, nothing has provided what I need).
WITH MEMBER [Measures].[count deliveries]
AS
COUNT(FILTER(DESCENDANTS([Date].[Calendar].[Year].&[2013],2), [Measures].[total sales] > 0) )
SELECT NON EMPTY {[Measures].[total sales]
, [Measures].[count deliveries]} ON 0
, NON EMPTY ([Customer].[Customer Name]
, {DESCENDANTS([Date].[Calendar].&[2013],2)}) ON 1
FROM [Sales]
THE END GOAL
I would like to have this added as a calculated member to the cube and not just in query format. Any help would be appreciated!

Related

PowerBI - calculation with 2 different dates

I have one data table from which I have to calculate 2 different KPIs, each KPI is tied to different date column.
"Creation Date" for KPI "Net Satisfaction Score" calculation and "Uni Date" for KPI "Response Rate" calculation.
"Date" from "Date table" used as field to filter time periods so I need to have a relation to that field.
table design
If I filter for results in September'22, I want to see "Net Satisfaction Score" calculated from all Ids with Creation date in SEP'22, and I want to see "Response Rate" calculated from all Ids with UniDate in SEP'22 (this means Ids 00004, 00007, 00009 and 00010 are not to be considered in Response Rate calculation).
What I have tried already:
Using more queries - one for Response Rate (with relation UniDate <-> Date) and second one for Net Satisfaction Score (with relation Creation Date <-> Date).
This worked, but if I want to go more into detail and see the results by country, the numbers don't show up correctly, as there is no relation on "Country" or whatever detail I want to split the result by.
Making relation based on IDs between the queries mentioned in "1)" - circular dependency error.
I am really out of ideas, but maybe some of you tried to solve this kind of issue already.
Sounds like you need to use USERELATIONSHIP within your measure.
You will need something like this
Net Satisfaction Score = CALCULATE(Sum('Net Satisfaction Table[Score]'),
USERELATIONSHIP('DateTable[Day]','Net Satisfaction Score Table[Created Date]))
You can obviously use sum, count, average...whatever you need to do with your score. You also need to make sure that both dates - Created Date and UniDate have an inactive relationship back to your Date Table.
Repeat the same measure example for your other measure. USERELATIONSHIP works perfectly on inactive relationships and only works within CALCULATE operation where you tell PowerBI which date to use in this calculation.

How to make all cohort weeks start from 100% mark in Line charts using Tableau?

I'm trying to build a weekly cohort analysis depicted by line charts in Tableau. The problem is that all weeks line should start from 100%.
Below is the process I took :
dropping order date to columns and selecting week number
COUNTD (order id) and in rows
creating a calculated field : first purchase date{ FIXED [User Id]:MIN([Order Date])} and then
Dropping first purchase date to color field
I'm getting weeks depicted as lines (different colors) but can't figure out a way to make all lines starting from 100% point.
Here is the screenshot of what it should look like https://prnt.sc/1uugad5
Link to the dummy data where order_id is unique Dummy Data link . Any help is appreciated
Thanks
To achieve the viz, I used COUNTD([User ID]) instead of order ID.
The x axis represent the difference of weeks between the order date and the cohort date.
Finally, the percent can be found using percent of total quick table calculation and using table across.

Tableau measure count items if between dates

What I am trying to achieve is to get a count of people employed in a particular period.
I have 3 variables:
Employee ID (integer)
Hire date (date)
Termination date (date or null)
Example
the formula I am looking for is something like
if termination_date is null
then
count employee_ID in
dates between Hire_date and max of either hire_date or termination_date
else
count employee ID in
dates between hire_date and termination_date
This aims to show the dynamic of staff level over the time.
I am new to Tableau, not sure how to even start with it. Any suggestions welcome.
This problem will be simpler if you reshape your data to have the following three columns
Employee ID
Date
Action. (where action takes on the values of ‘Hire’ or ‘Terminate’).
Each data row represents one change in status for an employees. If an employee had a termination date, they will have two records in this new format, otherwise just one record showing the hiring date.
You can reshape your data by hand, or leave the original and use Tableau Prep or the Tableau data source page to reshape using a self Union and a few simple calculated fields.
Define a calculated field called Staffing_Change as
if Action=‘Hire’ then 1 else -1 end
Now you can plot the change in staff level over time by putting exact date on columns and sum(Staffing_Change) on Rows. You can use a quick Table calc, Running Sum, to see the net staffing level. For line mark types, I’d use a step style by pressing on the path button on the Marks card. Otherwise, the chart can give the impression of fractional number of employees.

Calculated Field to Sum Two Values Tableau

Is there a way to sum up two values from two different sheets. I have one sheet that looks at full time students as a distinct count of their ID and on another sheet I made a calculated field that takes the contact hours of part time students and divides by 12 (FT course load). I want to be able to add up these two numbers so that the... SUM(Full Time + (Part Time Contact Hours/12)) = ### and would result in an FTE (full-time equivalent enrollment).
Try a calculations similar to these:
Full Time
IF [Type]="FullTime" THEN [StudentId] END
Part Time
IF [Type]!="FullTime" THEN [Hours] END
FTEs
COUNTD([Full Time]) + SUM([Part Time])/12
Obviously I don't know your data structure, hopefully that gives you enough to go on to start.

Count records as proportion of total in Tableau

I have data with sales, details including gender, location, date, etc. There's one row for each sale, so the total number of sales is a count of the rows.
customerid sale_date regionid studentid sex
18761372 01/09/2016 AFB07458 C2F815C6 1
18761372 01/09/2016 AFB07459 206AA234 0
07189635 01/09/2016 AFB07460 F218C8F1 1
07189635 01/09/2016 AFB07461 F021CD27 0
07189635 01/09/2016 AFB07462 E6145555 1
I'm trying to produce a line graph that shows number of sales by month, split by male and female. However male sales are ~5million for the year, and female are ~13 million. So it's hard to compare whether there's any difference in how the sales vary by month (i.e. whether males are proportionately more likely to buy in september).
I manage to get this to work by creating the following calculated field:
If [sex] = 0
THEN 1/5000000
ELSE 1/13000000
END
This does what I need, but it's not a great solution as it involves me manually checking what each total is and writing it down (admittedly not much of an issue with sex, but if I'm doing it on something with multiple fields then it would become very time consuming.
Is there a way of doing this more elegantly? I tried to use sums and case statements in there but I got "cannot mix aggregate and non-aggregate arguments".
I would approach this with a calculated field that shows a percent of total, by month.
Right click on your pill, select 'Quick Table Calculation' and choose 'Percent of Total'.
That way, instead of looking at a count of orders where one group always appears greater, you are looking at their share which will be more uniform.
Maybe I'm understanding this wrong but it seems fairly straight forward. Using the sample data provided I just:
Add the date field pill "sale date" to the column shelf(set it to pull back the month)
Drag the "number of records" to the rows shelf(automatically sum the results)
In the "Show Me" section in the top right select line chart.
Drag the sex pill onto the color card, this will break the line into to two, one for male the other for female(you may have to convert the sex pill to a dimension, to do this right click the pill and select "convert to dimension")
Let me know if this solves it for you?