TiBCO Spotfire - How to Calculate only the last 3 columns in a Data - see descr - tibco

Week Sales
1 100
2 250
3 350
4 145
5 987
6 26
7 32
8 156
I wanted to calculate the sales only for the last 3 weeks so the total will be 156+32+26.
If new weeks are added it should automatically calculate only the data from the last 3 rows.
Tried this formula but it is returning an incorrect sum
sum(sales) over (lastperiod(3(week))
https://i.stack.imgur.com/6Y7h7.jpg

If you want only the last 3 weeks sum in calculated column you can use a simple if calculation.
If([week]>(Max([week]) - 3),Sum([sales]),0)
If you need 3 weeks calculation throughout table use below one.
sum([sales]) OVER (LastPeriods(3,[week]))

Related

Amazon QuickSight - Running Difference

I have the following table and want to add a column with running difference.
time_gap
amounts
0
150
0.5
19
1.5
2
6
1
7
4
my desired out is
time_gap
amounts
diff
0
150
150
0.5
19
131
1.5
2
129
6
10
119
7
4
115
What I've tried:
I duplicate the amounts column and used table calculation, difference, but got the difference between two consecutive rows instead:
time_gap
amounts
diff
0
150
0.5
19
-131
1.5
2
-17
6
10
8
7
4
-6
I tried some calculated fields formulas but that didn't work either.
thank you!

PowerBI compare two columns in matrix

I am pretty new to PowerBI and cannot figure out a way to compare two values in two columns. All I need is a columns that says True or False. This would be pretty easy in Excel but I am sure there is a way in PowerBI as well?
My data looks like this:
Number
Type
Size
PO1
5
10
PO1
6
12
PO2
5
09
PO2
6
10
PO3
5
10
PO3
6
10
Which I organise to look like this in a matrix (not exactly this formatting but I don't know how to show this, but it's a matrix):
Number
Type 5
Size
Type 6
Size
PO1
5
10
6
12
PO2
5
09
6
10
PO3
5
10
6
10
And I want to know:
Number
Type 5
Size
Type 6
Size
Same size
PO1
5
10
6
12
False
PO2
5
09
6
10
False
PO3
5
10
6
10
True
You could solve this with a calculated table like:
Comparison =
SUMMARIZE(
'Table',
'Table'[Number],
"Same size", MIN('Table'[Size]) = MAX('Table'[Size])
)
which will look like this in a table visual:
What I did was Transform the data as follows:
Number as index column
Min size as 'input'
Max size as 'output'
New column comparing input and output
Save and Run

Add multiple custom row measures in PowerBI

I have a PowerBi matrix and I'm trying to 3 some custom rows at the end of each group but can't figure out how to do so. Below is what the matrix looks like.
Salesperson
Total Units Sold
John
Apples
10
Oranges
5
Spoilage
2
Katie
Mangoes
12
Apples
9
Pears
15
Spoilage
1
And I'm trying to get a Total, Net and Percentage into the matrix as shown below. Total Fruits is a summation of all the rows above except the spoilage row. Net is the summation of all above including the Spoilage and Percentage (Pct) is Spoilage divided by Total Fruits.
Salesperson
Total Units Sold
John
Apples
10
Oranges
5
Total Fruits
15
Spoilage
2
Net
13
Pct
13.3%
Katie
Mangoes
12
Apples
9
Pears
15
Total Fruits
36
Spoilage
1
Net
35
Pct
2.9%
I have a fact table that records each fruit sold by the product code and the salesperson id and dimension tables for the salesperson and the products.
I'm new to PowerBI and so I would appreciate all the details to make this work.

Algorithm to distribute evenly products value into care packages

i'm currently solving a problem that states:
A company filed for bankruptcy and decided to pay the employees with the last remaining valuable items in the company only if it can be distributed evenly among them so that all of them have at least received 1 item and that the difference between the employee carrying the most valuable items and the employee carrying the least valuable items can not exceed a certain value x;
Input:
First row contains number of employee;
Second row contains the x value so that the the difference between the employee carrying the most valuable items and the employee carrying the least valuable items can not exceed;
Third row contains all the items with their value;
Output:
First number is the least valuable basket of items value and the second is the most valuable basket;
Example:
Input:
5
4
2 5 3 11 4 3 1 15 7 8 10
Output:
13 15
Input:
5
4
1 1 1 11 1 3 1 2 7 8
Output:
NO (It's impossible to distribute evenly)
Input:
5
10
1 1 1 1
Output:
NO (It's impossible to distribute evenly)
My solution to resolve this problem taking the first input is to, sort the items in ascending or descending order so from
2 5 3 11 4 3 1 15 7 8 10 --> 1 2 3 3 4 5 7 8 10 11 15
then create an adjacency list or just store it in simple variables where we add the biggest number to the lowest basket while iterating the item values array
Element 0: 15
Element 1: 11 <- 3 (sum 14)
Element 2: 10 <- 3 (sum 13)
Element 3: 8 <- 4 <- 1 (sum 13)
Element 4: 7 <- 5 <- 2 (sum 14)
So that my solution will have O(nlogN + 2n), first part using merge sort and then finding max e min value, what do you guys think about this solution?

quick method count number of overlap intervals in an array of interval?

OK, this is a question I got for my advance algorithm class. I already turned in my solution once but got rejected by my instructor due to efficiency issue, in other words, I already made the efforts on my part but could not get it even after his hint, so please be gentle. I will give his hint below
Given an array of intervals with both start point and end point, find the number of other intervals fall within it for each interval. number of intervals is less than 10^9 and their ids are distinct. start and end are less than 10^18, the input files don't contain duplicate number for start and end. All the numbers above are integers
the hint is: considering a data structure with buckets. The algorithm should be faster than O(n^2)
sample input and output
input:
5 %% number of intervals
2 100 200 %% id, start,end. all lines below follows this
3 110 190
4 105 145
1 90 150
5 102 198
output:
3 0
4 0
1 1
5 2
2 3
The numbers are pretty big so O(N log N) might be a little to much but here's an idea.
First things first normalize the values, that means turning them smaller while keepinging the same ordering. In your example the normalize would be
90 100 102 105 110 145 150 190 198 200
1 2 3 4 5 6 7 8 9 10
So you're new intervals are:
5
2 2 10
3 5 8
4 4 6
1 1 7
5 3 9
Now the edges of the intervals are in the range of [1, 2N].
Now sort the intervals by their end:
5
4 4 6
1 1 7
3 5 8
5 3 9
2 2 10
When you reach an interval you can say that all the intervals that start before it and have not been encountered yet should have their answer increased by one. This can be done with a SegmentTree.
What you do when you get an interval [x, y] you increase all values in the range [1, x - 1] by 1 and then compute its answer as the value at x in the segment tree. That's just addition on an interval and query on a point, a common segment tree problem.
I don't really think you can solve this problem with less than O(N log N) time and O(N) memory, so this solution should be the asymptotically best solution in both time and space.

Resources