calculate_index function

Calculate index based on conditions