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

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.

Related

Using request.security data to use for calculations

I want to achieve the following...
For every candle, I want to calculate the "max range" of the previous x candles.
I then want to use this max range values and compare it with the ATR of the daily timeframe, to get the "max range" value as a percentage of the daily ATR
Point 1, was the easy part and I was able to achieve that with the below code.
// USER INPUTS
iLookBack = input.int(defval = 4, title = "Lookback Period")
// MAX RANGE OF THE LAST x CANDLES
maxRange = math.abs(ta.highest(iLookBack) - ta.lowest(iLookBack))
plot(maxRange, color = color.aqua)
Below a picture of it.
Point 2 is where I got stuck, and where your help is needed.
I tried fetching the daily ATR data using the request.security function
datr = request.security(syminfo.tickerid,"D", ta.atr(14))
On the current day, I get a DATR reading on all candles of the day.
But on the prior day, I only get a DATR reading on the FIRST CANDLE OF THE DAY.
So far so good...
But when using the DATR data and trying to plot the result of the calculation (below) I only get the result of the calculation plotted for the 1st candle of the day.
plot(maxRange/datr)
And the result is looking like this..
This is not what I want..
What I want is, that for every candle, I want to compare the "maxRange" with the DATR reading. And that this result is being plotted.
My guess is, that the data fetched using the request.security function is only valid for ONE candle at the lower timeframe? I'm actually not sure.
I looked up the documentation but couldn't find the answer to my problem..
I hope my problem is clear. And what I'm trying to achieve is making sense to you.
Thank you very much in advance.

Get Dynamic Date Difference in Power BI

I am trying to get the difference between dates based on selected date values from a slicer.
From my image, the minimum date selected on the slicer is 5/10/2022 and the period to is all within 2022 therefore I was expecting a difference of less than 365. But it's subtracting 1/1/2008 which is the minimum date for my dates table.
I created the DateX_col to see the minimum value being used; my intention is to have that column show the minimum value selected on the slicer. How can I achieve this?
I'm using MIN(CALENDAR[DATES]) to get the minimum value dynamically, and I'd also like the same for MAX values.
Also, DATEDIFF(MIN(CALENDAR[DATES]), PERIOD_TO,DAYS) is not picking from the slicer, rather it's picking from the minimum value of the table. So my column: DateX_col = MIN(CALENDAR[DATES]) is showing 1/1/2008 and my measure: date x = MIN(CALENDAR[DATES]) is showing 5/10/2022.
It is not possible to get a dynamic MIN column in Power BI.
We can get dynamic data differences by creating a measure([Periodto - Datex]) using the measure ([date x).
Periodto - Datex = DATEDIFF([date x], MIN(Table1[PERIOD_TO]), DAY)

Power BI - Timeseries compare two different start dates

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

Spotfire: Empty values on x-axis

Spotfire question: I have a data table with monthly data that is visualized in a bar chart. I however also want to visualize the information in quarters, by showing the latest month of the quarter in the format '20Q1, 20Q2, etc.'. (So I don't want to use standard 'date format'.)
My idea was to create an additional column that is filled for March, June, Sep, Dec and empty for the other months. Then with a document property, the user can select to either view the data in months or in quarters (i.e. the last month of the quarter).
So far so good, my data now looks like this:
Month
Value
YearQuarter
Jan-20
100
Feb-20
200
Mar-20
400
20Q1
Apr-20
125
May-20
101
Jun-20
300
20Q2
The problem now is that when I visualize the data with YearQuarter on the x-axis, it also shows all the (empty) values in a bucket. See below. How to solve this? Note that the x-axis has a custom expression "<$esc(${Granularity})>", where Granularity is a document property to determine what column to pick.
Did you try limiting your data with whatever expression you have put in x axis.
Thanks

Tableau 8.2 - how to get max and min from % difference values on table?

I'm facing problem in getting the max % and min % from a table containing % difference values.
Year-----A----------B---------C---------D---------Max %----Max Type----Min %----Min Type
2012
2013---4.30%---4.42%---4.34%---4.38%----4.42%---------B-----------4.30%---------A
The table above shows the % difference in sales from previous year. Thus 2012 shows no % (because there's no 2011). I used table calculation to compute the % difference, i.e. "Percent Difference From", compute using "Table (Down)" and "Previous".
The last four columns are what I'm having trouble doing. I want to get the max % and min % and also the corresponding types. I'm not trying to add the four columns to the existing table, but to get the correct results, as my ultimate goal is to display that results on the dashboard, i.e. on my dashboard, I want to display the highest % and its corresponding type; similarly the lowest % and its corresponding type. For example: on my dashboard, I want to display:
Highest % and type: 4.42% B
Lowest % and type: 4.30% A
So, I need to have the correct formulas to get the max % and min % and their types. These are what I did:
I tried to use WINDOW_MAX and WINDOW_MIN to display the max % and min % on the table but got funky wrong results.
1) I first get the formula in calculating the % difference from the "Customize" button from "Edit Table Calculation" window of SUM([Sales]): (ZN(SUM([Sales])) - LOOKUP(ZN(SUM([Sales])), -1)) / ABS(LOOKUP(ZN(SUM([Sales])), -1))
Then I created a calculated field of the above formula. I named the calculated field "Percent-Diff".
2) I created another calculated filed (named "Max % Difference") using the formula: WINDOW_MAX([Percent-Diff]). But it shows strange results. See image below. I don't know why it gives me 2.78% and 2.91% for 2012 and 2013 respectively. It should be 0% and 4.42% for 2012 and 2013 respectively. Something is not correct.
If it is just SUM([Sales]) instead of % difference, then I get the correct result of showing the max sales using the formula WINDOW_MAX(SUM([Sales])).
3) Also I don't know how to get the corresponding type. I tried using the formula: IF [Max % Difference] = [Percent-Diff] THEN ATTR([Product Type]). But it returns:
NULL
B
I'm not sure if the formula is correct. It looks correct on the result (i.e. "B" is correct), except that it also shows a NULL value which I don't know why. I think it's because I didn't include the ELSE part in my IF formula? But why the NULL value is shown as the first value? I want the formula to return just one value, "B". So, how to only just show "B"?
I've posted twice the problem in tableau forum, but as of now, nobody has answered my problem. I believe that my formulas are incorrect. So, if anyone here can correct the formulas to get the max % and min % from % difference values and also to get the corresponding type, then it'd be very much appreciated. Thanks a million!
It's hard to tell not knowing how your database looks like (as you didn't explicitly presented it, but I can try to infer based on the clues you left on your post). But I could reproduce something like you said using the Sample - Coffee Chain Database, and it worked out well, calculated yoy sales increase by product and then window_max of that.
What you're probably missing is the partitioning. I suggest avoiding using Table or Pane to create the partitions in more complex situations (as it will work only in that specific arrangement of fields), but rather use the dimensions to partition it.
So, your [Percent-Diff] field should be compute using [Date], and your [Max % Difference] should be compute using [Product Type]. IMPORTANT, for [Max % Difference], when you go to Edit Table Calculation, you'll have to choose the Compute using for [Percent-Diff] as well (you can choose on the top of the window)
Your formula to find which type is the max (or min) is also correct (and should only respect the partitions). Nevertheless, it is very hard to have the exact output you're expecting.
What I would do is to create 2 spreadsheets (and later combine them in a dashboard).
The 1st would be what you already got (Each product [Percent-Diff]
The second one I would change your formula (3) to just [Max % Difference] = [Percent-Diff], and use it as filter (filtering only true). I would drag both Date and Product to the sheet (you choose if you want it on columns, rows, or just detail) so I can use them to partition the table. And drag [Max % Difference] to be visualized.
That way you'll only see the product that is the max, and how much is that max.
Hope it helps