Power BI - Timeseries compare two different start dates - date

I want to compare how different campaigns are progressing based on number of days into the campaign rather than by date (see day1, day2, etc... on the x-axis below).
Here is my DAX code, but I can't get it to work. Any help would be much appreciated...
**Normalised Campaign Metrics =
VAR DateReached = CALCULATE(MIN(Days[Day]),db[PAYMENT_DATE]<> BLANK(), KEEPFILTERS(db[PRODUCT_CODE SWITCH]))
VAR MaxDate = CALCULATE(MAX(db[PAYMENT_DATE]),KEEPFILTERS(db[PRODUCT_CODE SWITCH]))
VAR DayNo = SELECTEDVALUE(Days[Day])
RETURN CALCULATE(count(db[PAYMENT_DATE]),
FILTER(ALL(db[PAYMENT_DATE]),
DateReached+DayNo && DateReached+DayNo<=MaxDate))**
Many thanks!
enter image description here

I would recommend solving this through manipulating your actual data rather than a complex DAX measure. If you are familiar with star schema modelling, I would solve this problem by adding a new column to your fact table that calculates how many days from the start date the payment occurred and then connect this column to a new "Days Passed" dimension that is simply a list of numbers from 1 to however many days you need. Then, you can use this new dimension as the source data for your x axis and use a standard payment amount measure for your y axis.

I recommend to create a dimension table as the relative basis to comparison with inactive relationship. Here is a video about it:
https://youtu.be/knXFVf2ipro

Related

How can i create a mongodb query that calculates the difference of a value along other timestamps using a unique id

I have collected various data via the Twitch API and would now like to prepare and display it in a mondodb chart.
What I want to display are the viewer counts along the measurement points (timestamps). The IDs per timestamp should be compared with the viewer count. If the viewer count is equal to the previous timestamp, nothing should happen, if the value has changed, the difference to the current value should be entered e.g. in a new field. At the end I want to see on the x axis the timestamps and on the y axis the viewer count for the timestamps
here is a axample of the Object. Eevery minute I get one such object per active livestream (mostly 20 objects per request/timestamp)
_id:604672c85f68ef1c71546c71
id:40931788941
user_id:493094533
game_name:"Science & Technology"
title:"Arduino Lasershow aus dem 3D-Drucker. #arduino #3D-Druck #CNC #Program..."
viewer_count:151
started_at:"2021-03-08T14:50:25Z"
language:"de"
time:2021-03-08T18:54:00.911+00:00
__v:0
most important attributes:
id:40931788941
time:2021-03-08T18:54:00.911+00:00
viewer_count:151
My problem is that I don't know exactly how to calculate the difference to the previous object with native mongo. If you have another solution, bring it to me ;-)
EDIT:
You can see in the following screenshot the x axis the dates and on the y the viewer_count for this date. But the viewer_count is not meaningful because its a sum of all viewer_count values of all objects for the specific date.
I only want to get the cumulative value of the viewer_count attribute for the specific date.
Other aggregations for this attribute do not help much here either.

Detecting patterns in data with Tableau

i'm totally new to Tableau but that is what I could potentially use at the workplace, so asking this question to decide if its worth it.
I've a monthly values dataset and I'd like the tool (Tableau) to generate a report to point out anomalies - the ones i have in mind right now are:
1) same data value for x months in a row
2) data value is 0
3) 5 parameters been reported last 3 months and all of sudden only 4 are reported
Is this possible in Tableau?
You can certainly do this. The below is a general example for doing anomaly detection (one standard deviation outside of mean) but you can modify the calc to fit your criteria. If you place your date field in the column position and your metric, lets say Sales, in the row position you can then create this calculated field to label the anomalies.
IF SUM([Sales]) < (WINDOW_AVG(SUM([Sales])) – WINDOW_STDEV(SUM([Sales]))) THEN “Bad
Anomaly”
ELSEIF SUM([Sales]) > (WINDOW_AVG(SUM([Sales])) + WINDOW_STDEV(SUM([Sales]))) THEN
“Good Anomaly”
ELSE “Expected”
END
You can then place the new field on your color mark to highlight each of the results.

Function which finds temperatures for a given month from data

I'm having some issues creating a function with the following parameters:
Ndata = extperiod(data, year, month,time)
The data is a table with 3 columns, which from left to right are:
year/month/date, time, temperature
My goal is to create a function which can extract a time and a year/month, irrespective of the date and find it's corresponding temperature.
I need to avoid using for loops
I've been advised to use floor and find, where floor(YYYYMMDD/100) = YYYY*100 + MM, which I somehow want to integrate to my function.
I've previously found a way to extract all temperatures from the data for a given day, as follows:
k = find(data(:,1)==19750101);
data(k(1):k(end),3)
I'm trying to incorporate this method, but I think that the hint "floor(YYYYMMDD/100)" throws me a of a little.
I have tried with find(data(:,1)==floor(YYYYMMDD/100)), where I would think that I'd be given all dates with a specific year and month. For example:
find( data(:,1) == floor(19660101/100) )
I thought this would give me all points in the column vector where the value is 196601. But it doesn't.
What could I try differently?
From your explanation, your want to get all temperature for a given month, no matter time and day.
So you want to find dates that are comprised in the range [YYYYMM ; YYYY{MM+1}[ or [YYYYMM ; {YYYY+1}01[ in the case of selecting December.
Recall that you store the complete date in your table. So you need to apply your operator floor to both sides of your query, not only on the query value, because no date is floor(YYYYMMDD/100)!
As a result, try the following:
find( floor(data(:,1)/100) == floor(19660101/100) )

Creating a "stacked" Gantt chart in Tableau?

I'm working with a dataset that describes contract durations with a list of companies. Obviously all contracts have a start date and a (minimum) duration. Some contracts however, are subject to possible prolongation. Here's a data example:
Company contractStart minDuration firstProlong secProlong
Company x 27-11-2011 27-11-2014 27-11-2015 27-11-2016
Company y 29-11-2014 29-11-2015
Company z 01-04-2011 01-12-2011 01-12-2015 01-12-2017
I want to visualize them in a Gantt'ish type diagram that has duration along the x-axis, and shows the respective contract lengths with the possible prolongations as a sort of stacked bar chart. Along with that I want something to mark the current date. I've managed to do this in Excel. See picture:
.
I would however really like to be able to the same thing in Tableau, and I really can't figure out how to. I've been working with two different approaches so far:
1st approach
Here I drag the Company to the Rows shelf, and the contractStart date to the Columns shelf. I then create a calculated field that calculates (with DATEDIFF) the difference in days between the contract start and the minimum duration. I can drag that to the Size shelf, set the mark type to Gantt bar, and get something like this:
I can then go on to calculate the date difference between the minimum duration and the 1st prolongation and the same for the 1st and 2nd duration. The problem here is that I can't add these calculated fields to the Size shelf as it can only hold one at a time.
2nd approach
Here i reorganize my data like this:
Company Type startDate endDate
Company x minDuration 27-11-2011 27-11-2014
Company x firstProlong 27-11-2014 27-11-2015
Company x secProlong 27-11-2015 27-11-2016
Company y minDuration 29-11-2014 29-11-2015
Company y firstProlong
Company y secProlong
Company z minDuration 01-04-2011 01-12-2011
Company z firstProlong 01-12-2011 01-12-2015
Company z secProlong 01-12-2015 01-12-2015
I then create a calculated field to find the difference in days between startDate and endDate. I drag that to the Columns shelf and drag Company to the Rows shelf. I then drag Type to the Color shelf, set the mark type to Bar, and get something like this:
The problem here is that I need dates along the x-axis not integers. Also I need to be able to sort the chart like my initial Excel example. I.e. I want to sort on the longest possible duration of a contract (ascending). Also having to reorganize all my data seems a bit tidious.
Here's the spreadsheet and the tableau file I've been working with. Am I overlooking something really simple?
Your approach 2 is close. instead of sum(datediff) on the columns, place StartDate as continuous. Then place sum(datediff) on size. You can now sort Company ascending on sum(datediff). Sometimes, reorganizing data is necessary.

Removing non-matching dates from two time series in matlab

I have two time series x and y which roughly cover the same period of time. The data is in daily form however there are some days that have data in one dataset but no data in the other. I wish to use matlab to create two data-sets of equal size with matching dates. Essentially I wish to remove the days that don't have data in both x and y. Is there a simple way to do this? Thanks.
You could use an inner join see help join if you are able to convert your timeseries into datasets. If not you could use the ismember function, but this time you should do it only on the dates.
Something like this will work:
a = {'2015-01-01', '2015-02-02', '2015-03-03'};
b = {'2015-01-01', '2015-03-03', '2015-04-04'};
newA = a(ismember(a,b));
newB = b(ismember(b,a));