1

[Tip] Organizing your codes with additional Data.

I wanted to share a tip that has become very useful lately for me. 

Imagine you have an input control and your data may vary if you choose "daily", "weekly" or "monthly". You could also have a large formula that selects a range of dates. Anything that makes your code difficult to read. 

In one of my Klips, I have 3 user inputs: Date from, date to and period (weekly, daily or monthly).

So my "date from" and "date to" would interact to form a date range that I would use with select function to get data only from that period or groupby, like this:


you can end having something like this:


You can have all the code that is repeated again and again just in an extra data and reference it, using it kind of a s a function.



All my series have the same structure, so I use the same data for all of them (dates are the same for all). I then can in other Klips just copy and paste my codes in these data and create the same for similar klips and so. This way cleaning the code. Another person that sees your code now will be able to understand it if you name your data correctly. There is no need to see what is actually inside this black boxes.

I hope this helps, cheers!

 

 

 

7 comments

  • Avatar
    Janice Janczyn Official comment

    Hi Gianfranco,

    This is a nice way to make your formulas more readable and easier to maintain. However, references are not functions: each reference is fully expanded so be mindful of performance. In your example, the formulas in Date divison and Selection are recalculated each time they are referenced as well as in their hidden data subcomponents.

    Happy dashboarding!

    Janice

  • 0
    Avatar
    Jon S

    Great tip! Thank you for sharing. :)

  • 0
    Avatar
    Gianfranco Gallese

    Janice,

    What do you mean with that the formulas are recalculated?

    Thanks for the feedback,

    Gianfranco

  • 0
    Avatar
    Janice Janczyn

    Hi Gianfranco,

    In the Klip Editor, a reference to the date division hidden data subcomponent is displayed in the formula as !date division, but in the underlying source code, the formula contained in that reference 

        GROUPBY( SWITCH(.....),

                            SELECT(....),

                            SUM )

    is fully expanded and calculated every time !date division is referenced. So there is no performance savings when using REFs and in your scenario, the formula is actually calculated 1 extra time, in the hidden data subcomponent. REFs simplify the appearance of a formula and ensure consistency when the same formula is repeated. Also, it simplifies maintenance if the formula is modified, it only needs to be updated in 1 place. When building your formulas, it  is important to remember that REFs are fully expanded when a formula is calculated, because sometimes a formula is more efficient without a REF. 

    Thanks,

            Janice

  • 0
    Avatar
    Gianfranco Gallese

    Hi Janice,

    Thanks for your reply, I understand now. I thought you meant that it was less efficient than just writing it all together, but it seems to be the same then. It is still very useful to have the data organized, that is what I intended, the code is much easier to understand and it is not slower. Am I right? Now that you mentioned it, is there a better way to do this, more efficiently?

    Thanks!

    Gianfranco

  • 0
    Avatar
    Janice Janczyn

    Hi Gianfranco, 

    In your example, it is slightly less efficient to use REFs because the formula is calculated 1 extra time, however in this case, the trade-off is worth it here, for the simplicity and maintainability that you gain. 

    A more efficient solution may be to use Applied Actions on your Hidden Data columns, which now support GROUPing, instead of the GROUP and GROUPBY functions.

    Thanks,

            Janice

     

  • 0
    Avatar
    Josh Cohen-Collier

    Hi All!

    Just a quick update to what Janice mentioned earlier, about component references actually being recalculated each time.
    We have a new feature, called the results reference, which gets the output of a compoennt, after any applied filtering, grouping, or sorting is considered.

    However, even if there are no applied actions, it keeps the value stored, and is faster than recalculating the formula, like the component reference.
    This can be used to both simplify your formulas, and also speed up loading your Klips!

    Hope this helps, and happy Klipping!

    Best regards,
    Joshua
    _________

    Joshua Cohen-Collier
    Technical Support Engineer, Klipfollio

Please sign in to leave a comment.