LASTINDEXOF


Finds the last instance of a substring in a text and returns its index.

Syntax:

LASTINDEXOF(text, substring)


text is what you are looking at.


substring is what you are searching for in text.


Example:

If A1 contains "Hello" and A2 contains "l":

LASTINDEXOF(A1, A2)

returns 3


Text:


Substring:


Result:

3

Application:

Analyzing Website User Data


Imagine you are a data analyst for an e-commerce website. You have a log of user actions, and you want to find the last time a specific user viewed a product page. This could be useful for a variety of reasons, such as:

  • Remarketing: You want to show a targeted ad for a product the user recently viewed.
  • Customer Support: A user calls with a question about a product, and you want to quickly see the last time they were looking at it.
  • Behavioral Analysis: You want to understand the typical path a user takes before making a purchase.


Let's say your data is stored in a simple array of strings, where each string represents a sequence of user actions for a single session.

User Action Log

The following table shows a log of actions for a user named "Alice". Each entry in the Session Actions column is a string representing the pages she visited, in order.

Session ID

User

Session Actions

A
B
C
1
101
Alice
homepage,category,productA,cart
2
102
Alice
homepage,search,productB,productA
3
103
Alice
homepage,category,productC,productA,productB,checkout
4
104
Alice
homepage,productA,productA

Using LASTINDEXOF

Your goal is to find the last time the string "productA" appears in each session's action log. The LASTINDEXOF function is perfect for this. It returns the index of the last occurrence of a specified substring within a string. If the substring is not found, it returns -1.


Let's apply LASTINDEXOF to the "Session Actions" for each session.

  • Session 101:
    • "homepage,category,productA,cart"
    • LASTINDEXOF("homepage,category,productA,cart", "productA") will return 19. This is the starting index of the only occurrence of "productA".
  • Session 102:
    • "homepage,search,productB,productA"
    • LASTINDEXOF("homepage,search,productB,productA", "productA") will return 24. This is the index of the last occurrence of "productA".
  • Session 103:
    • "homepage,category,productC,productA,productB,checkout"
    • LASTINDEXOF("homepage,category,productC,productA,productB,checkout", "productA") will return 27. This is the index of the last "productA" entry.
  • Session 104:
    • "homepage,productA,productA"
    • LASTINDEXOF("homepage,productA,productA", "productA") will return 18. This is the index of the second "productA", which is the last one in the string.

Result Table

Here is the updated table, showing the result of the LASTINDEXOF function for each session.

Session ID

User

Session Actions

LASTINDEXOF("productA")

A
B
C
D
1
101
Alice
homepage,category,productA,cart
18
2
102
Alice
homepage,search,productB,productA
25
3
103
Alice
homepage,category,productC,productA,productB,checkout
27
4
104
Alice
homepage,productA,productA
18




This page is protected by Google reCAPTCHA. Privacy - Terms.
 
Built using Zapof