Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
There are various loops/control statements that can be used in a script to control script execution. Some common ones include:
It is also possible to use one loop/control statement within another loop. In the script I am going to review in this blog, I used all of these control statements except the do…loop to generate the rank of a company, by region, over 8 years. Let me begin by setting the scene and explaining what I am trying to do. I had 8 years’ worth of company data that included the company's overall rank (in a Data table). Let’s say the overall rank is the company’s rank based on sales across all regions for a given year. My goal is to get a company’s rank for their specific region, each year, based on the overall rank. For example, in the example data table below Company C may have an overall rank of 3 in 2017 but in its region, North America, it is the top company so it has a regional rank of 1.
Example Data
Year Company Overall Rank Region Regional Rank
2017 | Company A | 1 | Europe | 1 |
2017 | Company B | 2 | Europe | 2 |
2017 | Company C | 3 | North America | 1 |
2017 | Company D | 4 | Central America | 1 |
2017 | Company E | 5 | North America | 2 |
2017 | Company F | 6 | Europe | 3 |
2017 | Company G | 7 | Europe | 4 |
What I would like to do via the script is determine the regional rank for each company, each year, so I can use it in a KPI object in an app. Here is the script that I am using to accomplish this:
Let’s step through the script step by step.
Save yourself time and lines of script by using loops when you need to repeat one or a series of statements in your script. Loops can be nested, as I did in the script above, or used one at a time. Instead of repeating the same statements for each year and each region, use nested loops so the statements in the script only need to be written once. This also makes maintenance easier. In the future, if a change needs to be made to the script, it only needs to be done once. Check out other blogs about loops with Henric Cronstrom’s blog on Loops in the Script and my blog on the Do…Loop.
Thanks,
Jennell
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.