Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X
Good day Community,
I have an Info table that stores the sales of fruit a day. See (MyTable.png)
<MyTable>
I want to add the number of fruits I sold each day then reorganize my table to that the day that has the highest sales can appear at the top of my list, and the day I on which I had the least sales should appear at the bottom of the info table. See the image below:
I've written up a piece of code, however I have hit a wall because the sum of the sales per day is not correct. How can I fix this:
let Total_Sales_Per_Day = 0;
for(let c=0; c<Fruit_Names.length; c++){//the column names from the input
Fruit_Input = Fruit_Names[c].Fruit_Names;
Value = MyTable[Fruit_Input];
Value = parseFloat(Value);
let Total_Sales_Per_Day = Total_Sales_Per_Day + Value;
}
let result = Total_Sales_Per_Day
Regards,
Solved! Go to Solution.
Hi @Janicen
Please try below code
// result: INFOTABLE dataShape: ""
let data = me.GetDataTableEntries({
maxItems: 10 /* NUMBER {"defaultValue":500} */
});
// Add Total field to infotable to store total value
let newField = {
name: "Total",
baseType: 'INTEGER'
};
data.AddField(newField);
// Iterate over infotable data
for(var i = 0; i < data.length; i++)
{
// Add Orange and Apple Count
var total = data.rows[i].Orange + data.rows[i].Apple;
// Update Total value to infotable
data.rows[i].Total = total;
}
// Sort infotable based on highest total
let params = {
sortColumn: "Total" /* STRING */,
t: data /* INFOTABLE */,
ascending: false /* BOOLEAN {"defaultValue":true} */
};
// result: INFOTABLE
let result = Resources["InfoTableFunctions"].Sort(params);
// To get the highest sale day
// let result = data.Day // Output will be STRING and rename sort 'result' to 'data'
The output will look like this:
Or if you want only the highest sale day
// result: INFOTABLE dataShape: ""
let data = me.GetDataTableEntries({
maxItems: 10 /* NUMBER {"defaultValue":500} */
});
/************************************************/
var hightestTotal = 0;
var hightestSaleDay = '';
// Iterate over infotable data
for(var i = 0; i < data.length; i++)
{
// Add Orange and Apple Count
var total = data.rows[i].Orange + data.rows[i].Apple;
if(total > hightestTotal)
{
hightestSaleDay = data.rows[i].Day;
hightestTotal = total;
}
}
var result = hightestSaleDay;
/VR
Hi @Janicen
Please try below code
// result: INFOTABLE dataShape: ""
let data = me.GetDataTableEntries({
maxItems: 10 /* NUMBER {"defaultValue":500} */
});
// Add Total field to infotable to store total value
let newField = {
name: "Total",
baseType: 'INTEGER'
};
data.AddField(newField);
// Iterate over infotable data
for(var i = 0; i < data.length; i++)
{
// Add Orange and Apple Count
var total = data.rows[i].Orange + data.rows[i].Apple;
// Update Total value to infotable
data.rows[i].Total = total;
}
// Sort infotable based on highest total
let params = {
sortColumn: "Total" /* STRING */,
t: data /* INFOTABLE */,
ascending: false /* BOOLEAN {"defaultValue":true} */
};
// result: INFOTABLE
let result = Resources["InfoTableFunctions"].Sort(params);
// To get the highest sale day
// let result = data.Day // Output will be STRING and rename sort 'result' to 'data'
The output will look like this:
Or if you want only the highest sale day
// result: INFOTABLE dataShape: ""
let data = me.GetDataTableEntries({
maxItems: 10 /* NUMBER {"defaultValue":500} */
});
/************************************************/
var hightestTotal = 0;
var hightestSaleDay = '';
// Iterate over infotable data
for(var i = 0; i < data.length; i++)
{
// Add Orange and Apple Count
var total = data.rows[i].Orange + data.rows[i].Apple;
if(total > hightestTotal)
{
hightestSaleDay = data.rows[i].Day;
hightestTotal = total;
}
}
var result = hightestSaleDay;
/VR