Cumulative sum by category with DAX (Without Date dimension) - dax

This is the input data (Let's suppose that I have 14 different products), I need to calculate with DAX, cumulative Total products by Status
ProductID
Days Since LastPurchase
Status
307255900
76
60 - 180 days
525220000
59
30 - 60 days
209500000
20
< 30 days
312969600
151
60 - 180 days
249300000
52
30 - 60 days
210100000
52
30 - 60 days
304851400
150
60 - 180 days
304851600
150
60 - 180 days
314152700
367
> 180 days
405300000
90
60 - 180 days
314692300
90
60 - 180 days
314692400
53
30 - 60 days
524270000
213
> 180 days
524280000
213
> 180 days
Desire ouput:
Status
Cumulative Count
< 30 days
1
> 180 days
4
30 - 60 days
8
60 - 180 days
14

That's trivial: Just take the build in Quick measure "Running total", see screenshot.
The resulting table will look like this:
However, when you think about it, from a data point of view a sort order like the following makes more sense than ordering "status" by alphabet,
and finally you can take it straight away without any crude categorization

Related

How to get hour equally weight average in Amazon QuickSight?

I want to aggregate value as hour equally weight average not general average.
Let's say I have the following dataset.
datetime
HH
value
2022-01-01T14:20
14
100
2022-01-01T14:30
14
80
2022-01-02T14:50
14
30
2022-01-01T15:00
15
60
2022-01-01T15:10
15
60
2022-01-01T15:15
15
60
2022-01-02T15:25
15
0
And I want to get this result.
HH
hour equally weight average
14
60
15
30
Calculation example for HH=14
General average calculation:(100 + 80 + 30)/3 = 70
But I want to get hour equally weight average:(100 + 80)/2 + 30/1 = 60
Calculation example for HH=15
General average calculation: (60 + 60 + 60 + 0)/4 = 45
hour equally weight average: (60 + 60 + 60)/3 + 0/1 = 30
I tried to use avgOver and sumOver, but failed.
Please help

Query table and group results into ranges in Laravel

I have a database table that stores multiple records of survey scores, the scores are between 1-100. I'm trying to present a frequency distribution on the apps front end, by grouping the scores into the following range;
Less than 20
20-30
30-40
40-50
50-60
60-70
70-80
80-90
90-100
So if the table had the data 87, 92, 95, 98, the user would see
80 - 90 (1)
90 - 100 (3)
etc. I think collections are the way to go about it, but I don't know where to start to get this sort of output, or whether it's even possible in Laravel?
Yes, it's possible. I believe this is the SQL query that you need (assume your table name is "scores", and "score" is the appropriate field):
select (case when score between 0 and 20 then 'Less than 20'
when score between 21 and 30 then 'Between 21 and 30'
when score between 31 and 40 then 'Between 31 and 40'
when score between 41 and 50 then 'Between 41 and 50'
when score between 51 and 60 then 'Between 51 and 60'
when score between 61 and 70 then 'Between 61 and 70'
when score between 71 and 80 then 'Between 71 and 80'
when score between 81 and 90 then 'Between 81 and 90'
when score between 91 and 100 then 'Between 91 and 100'
end) as score_range, count(*) as count
from scores
group by score_range
order by min(score);
So for Laravel it could work like this:
$frequency = DB::select("SELECT (CASE
WHEN score BETWEEN 0 AND 20 THEN 'Less than 20'
WHEN score BETWEEN 21 AND 30 THEN '20-30'
WHEN score BETWEEN 31 AND 40 THEN '30-40'
WHEN score BETWEEN 41 AND 50 THEN '40-50'
WHEN score BETWEEN 51 AND 60 THEN '50-60'
WHEN score BETWEEN 61 AND 70 THEN '60-70'
WHEN score BETWEEN 71 AND 80 THEN '70-80'
WHEN score BETWEEN 81 AND 90 THEN '80-90'
WHEN score BETWEEN 91 AND 100 THEN '90-100'
END) AS score_range, COUNT(*) as count
FROM scores
GROUP BY score_range
ORDER BY MIN(score);");
You can just edit the text titles.
In this query "40-50" (for example) it means, that the score is between 41 and 50. Also you can replace "ORDER BY MIN(score)" to "ORDER BY count" if you want.

How to calculate which second of song will be playing after x minutes of repeating?

I am not doing pretty well with algorithms, so I need your help :)
Case: I play a song that lasts 3 minutes 40 seconds at 1st August 00:00 (for example). How could I be able to calculate which second of the song will be playing after (again, for expample) 3 days, 7 hours, 3 minutes and 54 seconds or any other time interval?
Sorry if it sounds lame :(
That's a use case for the modulo operator, that gives the remainder of a division.
10 / 3 is 3 * 3 + 1. The +1 is the remainder
It's as simple as :
SecondOfTheSongCurrentlyPlaying = TotalSecondsElapsed % LengthOfTheSongInSeconds
In example, if the song lasts 3 minutes 40 seconds, this is 220 seconds.
3 days, 7 hours, 3 minutes and 54 seconds are 284 634 seconds.
284634 % 220 == 174 seconds

How can I modify a 2d matrix so that predefined sums in both dimensions are satisfied?

I'm working on optimizing production of a number of related widgets over a number of weeks. The total quantity of each widget, and the total quantity of widgets produced each week is fixed. By default, a few of each widget is produced each week.
For example:
Week: 1 2 3 4 5 Total
Widget A: 10 10 20 10 10 60
Widget B: 20 20 40 20 20 120
Widget C: 15 10 5 15 15 60
Totals: 45 40 65 45 45 240
However, due to overhead and setup time in the factory, I'd like the ability to reduce the number of types of widgets produced each week. For example, I'd like the user to be able to delete a number of the weekly widget runs, like this:
Week: 1 2 3 4 5 Total
Widget A: 10 __ 20 10 10 60
Widget B: 20 20 40 __ 20 120
Widget C: 15 __ 5 __ 15 60
Totals: 45 40 65 45 45 240
Given the above input, how could I code a solution to modify the numbers produced per widget per week, so that the total quantity produced per week, and the total quantity produced per widget overall, still satisfies the original totals?

Is it possible to find maximum value of 2 or more column in a table?

for example : I have a table as follows
id math science english history
1 80 90 90 90
2 70 60 81 78
3 69 50 45 80
4 30 40 10 80
i only want to find the maximum value in column math and science.
Is it possible?
Simply use this :
select max(science),max(math) from your_table

Resources