Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Input table:
Month | Inv Value |
Jan | 100 |
Feb | 200 |
Jun | 500 |
Sep | 300 |
Dec |
600 |
Output Table:
Month | Value |
Jan | 100 |
Feb | 200 |
Mar | 200 |
Apr | 200 |
May | 200 |
Jun | 500 |
Jul | 500 |
Aug | 500 |
Sep | 300 |
Oct | 300 |
Nov | 300 |
Dec | 600 |
I hope I'm understanding your issue- it looks you want to fill the missing months with the value from the most recent month
// Generate a table with Month Names
// Define the following line if not available in the Main section
// SET MonthNames='Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec';
Temp:
LOAD
SubField('$(MonthNames)',';') as Month
inline [
DUMMY_COL
1
];
// Add the Inv Values available
Left Join
Load * inline [
Month,Inv Value
Jan,100
Feb,200
Jun,500
Sep,300
Dec,600
];
// Use peek() to get the previous month's non-null value
NoConcatenate
Final:
Load Month,
if (isnull([Inv Value]),peek('Inv Value'),[Inv Value]) as [Inv Value]
Resident Temp;
drop table Temp;
exit Script;
If I'm understanding your problem correctly- you want the missing months to be populated with the most recent month value. Please see if this works for you.
// Generate a table with Month Names
// Define the following line if not available in the Main section
// SET MonthNames='Jan;Feb;Mar;Apr;May;Jun;Jul;Aug;Sep;Oct;Nov;Dec';
Temp:
LOAD
SubField('$(MonthNames)',';') as Month
inline [
DUMMY_COL
1
];
// Add the Inv Values available
Left Join
Load * inline [
Month,Inv Value
Jan,100
Feb,200
Jun,500
Sep,300
Dec,600
];
// Use peek() to get the previous month's non-null value
NoConcatenate
Final:
Load Month,
if (isnull([Inv Value]),peek('Inv Value'),[Inv Value]) as [Inv Value]
Resident Temp;
drop table Temp;
exit Script;
I believe the question is: how do you define these values?
- Do they have to change by year or Month, it's fixed or is there a rule you have to follow, you need to explain better what you want
I'm assuming you also have a year field and only want the months between the min and max dates.
You can use this code to fill the missing values while also formatting the date as such:
// Load Data and create a continuous Numbering for the Months
Data:
NoConcatenate Load
"Inv Value",
Year * 12 + Month(Date#(Year &' '& Month, 'YYYY MMM')) - 1 as MonthNum // Subtract 1 so all Mod(MonthNum, 12) = 0 for Jan and Floor(MonthNum / 12) = Year
Inline [
Year, Month, Inv Value
2023, Jan, 100
2023, Feb, 200
2023, Jun, 500
2023, Sep, 300
2023, Dec, 600
];
// Expand Table with all possible Date Combinations
MinMaxMonthNum:
NoConcatenate Load
Min(MonthNum) as MinMonthNum,
Max(MonthNum) as MaxMonthNum
Resident Data;
Let vMinMonthNum = Peek('MinMonthNum');
Let vMaxMonthNum = Peek('MaxMonthNum');
Drop Table MinMaxMonthNum;
Join(Data) Load
*,
MakeDate(Floor(MonthNum / 12), Mod(MonthNum, 12) + 1) as Date;
Load
RecNo() + $(vMinMonthNum) - 1 as MonthNum
AutoGenerate vMaxMonthNum - vMinMonthNum + 1;
Let vMinMonthNum;
Let vMaxMonthNum;
// Load final Table
Final:
NoConcatenate Load
Date,
If(IsNull("Inv Value"), Peek('Inv Value'), "Inv Value") as "Inv Value"
Resident Data
Order By MonthNum;
Drop Table Data;