How to get Last N Rows of any Infotable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
How to get Last N Rows of any Infotable
Hi Team,
Just like to get N no of rows from top in any infotable ThingWorx provides default snippet as TopN method of InfotableFunctions , do we have any snippet to get Last N rows of any infotable?
Please share the snippet.
Thanks in advance.
Solved! Go to Solution.
- Labels:
-
Coding
-
Examples
-
TWX Dev Portal
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@VladimirRosu @TonyZhang @VladimirN
For now I have ignored the rows from starting which I don't want in my final infotable via for loop but it will be good to have some method which can provide Last N rows also of an infotable just like TopN method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hi,
There was a similar question before - "How to get certain number of rows from Infotable": https://community.ptc.com/t5/ThingWorx-Developers/How-to-get-certain-number-of-rows-from-Infotable/td-p/610708
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@VladimirN The link shared is for First N Rows the method is TopN. But I need Last N Rows of an infotable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Currently there's no LastN or BottomN snippet available but might be released in the future.
You can post your idea in below site.
https://community.ptc.com/t5/ThingWorx-Ideas/idb-p/thingworxideas
For now you can consider using built-in Sort function (snippet) to sort on a column in descending order so you get your infotable rearranged in reversed order.
Then use TopN to extract the rows you want.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
And a (very simple) code-based alternative is the following:
// Simulation.DataShape is a custom DataShape I used for this test
let result = DataShapes["Simulation.DataShape"].CreateValues();
// test is the input infotable
let length = test.getRowCount();
// slice is a built in Javascript array method that will get us the last 2 rows in this situation; replace 2 with the desired number of last rows
test.rows.toArray().slice(length-2,length).forEach(row=>{result.addRow(row);});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
@VladimirRosu @TonyZhang @VladimirN
For now I have ignored the rows from starting which I don't want in my final infotable via for loop but it will be good to have some method which can provide Last N rows also of an infotable just like TopN method.